Flutter cross-platform framework has a low threshold of entry, good development efficiency and operation efficiency. There is a 77.6 K STAR on Github, so there is a reference in the project to achieve cross-platform requirements. On the whole, the project mainly encountered the following problems for reference:

Multilanguage class S instance is null

Default I18N library, using the locale class “S” to manage all language characters, dynamically generate S, using the form of JSON configuration file, dynamically generate all language corresponding variables. As shown in the figure:

For the default i18N library, calling r.f (context) returns NULL. Load (Locale(“en”)); To initialize S.

Therefore, create a new “R” and inherit “S”. In the initialization method of R, call s.telegate.load () first according to the current language environment, which can solve the crash problem caused by the initialization failure of S.

2. The problem of transferring strings from Android languages into JSON files of Flutter language;

Because Android has a full strings raw translation package, but Flutter only supports JSON, and the lattices must be:

Android native XML file format:

Therefore, it is a good idea to have a tool that automatically converts Android Strings. XML into a JSON file that conforms to the Flutter format, so that no one can copy and paste it, which is laborious, error-prone and time-consuming. The gadget has been posted to the extranet: strings. XML to Flutter JSON tool

The strings solved by strings. XML are escaped. When they are read and inserted into JS objects, json. stringify cannot be escaped again, otherwise they will be copied into the Flutter project and the compilation will fail.

String concatenation is used here:

      for (let key in obj) {
        json += "\n";
        json += JSON.stringify(key);
        json += ":";
        let v = obj[key];
        if (v.indexOf("\" ") != -1 && v.indexOf("\ \ \" ") == -1) {
          v = v.replace(/\"/g, "\ \ \"");
        }
        v = v.replace(/\\@/g, "@");
        json += "\" " + v + "\",";
      }
Copy the code

Also need to add Uyghur, multiple parameters when the same parameter name hotfix.

2. When referencing external files, use ‘package:’.

In a project, if a relative path is adopted and an external file is imported, and the class is a singleton, the singleton will be invalid and cannot play the role of state saving. Such as: import ‘.. /manager/VmDataManager.dart’; Should use: unified import ‘package: yh_flutter/manager/VmDataManager dart’;

In iOS, FlutterViewController self pointer is used as binaryMessenger variable, causing circular reference problems.

The code is as follows:

 FlutterMethodChannel * channel = [FlutterMethodChannel methodChannelWithName:@"com.xxx.client/plugin" binaryMessenger:self];
Copy the code

The FlutterMethodChannel member variable and The FlutterEventChannel member variable should be set to nil when the former ViewControlelr goes off the stack:

- (void)didMoveToParentViewController:(UIViewController *)parent{
    if(parent == nil) { self.settingPresenter.methodChannel = nil; self.settingPresenter.eventChannel = nil; self.settingPresenter = nil; }}Copy the code


Flutter does not allow StatefulWidget to inherit StatefulWidget components. Instead, Flutter should be extended to achieve similar reuse requirements of Java classes.

class _AboutPageState extends State<AboutPage> with BasePageMixin {}
Copy the code

BasePageMixin implements effects similar to the base class, such as “initialize navigationBar, return to previous page”, etc.

class BasePageMixin {
  Widget getAppBar(BuildContext context, String title, List actions) {
    return widget;
  }

  void back(BuildContext context){
    if (VmDataManager.getInstance().isStackTop(context)){
      return;
    }
    debugPrint("can pop="+ context.toString()); Navigator.pop(context); }}Copy the code




V. The effect of native control of the home page of Flutter and synchronous transfer of messages to Flutter can be realized by setting initialRoute.

Setting routing code on iOS:

[self setInitialRoute:self.routes];
Copy the code

Android setup routing code:

  public FlutterView createFlutterView(Context context) {
    WindowManager.LayoutParams matchParent = new WindowManager.LayoutParams(-1, -1);
    FlutterNativeView nativeView = this.createFlutterNativeView();
    FlutterView flutterView = new FlutterView(this, (AttributeSet) null, nativeView);
//    flutterView.setInitialRoute("xxx");

    flutterView.setLayoutParams(matchParent);
    this.setContentView(flutterView);
    return flutterView;
  }
Copy the code

The Flutter side reads the route string code:

window.defaultRouteName
Copy the code

SetInitialRoute can convey information that should be obtained when a Flutter is initialized, such as “language environment, login or not, login method, cache size, etc.”, which can reduce the process of Flutter asynchronously adjusting to native and native asynchronously adjusting to Flutter.

6. Flutter network can prevent HTTPS packet capture and self-signed root certificate trust problems.

Flutter network requests. The system proxy Settings will not take effect if the current proxy IP and port are not declared in the project code beforehand. Therefore, as long as the distributed package does not set the proxy method, it cannot be captured by third-party applications. The code is as follows;

 var client = HttpClient(context: context);
    client.findProxy = (uri) {
    return "The PROXY 100.69.181.11:8888";
 };
Copy the code

The company requires support for the xxx.cer root certificate, which is self-signed, so the root certificate trust is added. The code is as follows:

ByteData data = await rootBundle.load('assets/b.crt');
SecurityContext context = SecurityContext.defaultContext;
context.setTrustedCertificatesBytes(data.buffer.asUint8List());
Copy the code

Convert the CER certificate to x509 CRT format. The command is as follows:

openssl x509 -inform DER -in 053-Actalis_root.cer -out b.crt
Copy the code