前言

解决方案:

1、在pubspec.yaml里配置依赖packages

# 依赖库
dependencies:
  flutter:
    sdk: flutter
  #数据存储
  shared_preferences: ^0.5.2+1
  #获取版本号等信息
  package_info: ^0.4.0+3 

2、使用一个工具类持有一个静态的shared_preferences和package_info

import 'package:shared_preferences/shared_preferences.dart';
import 'package:package_info/package_info.dart';
class SpUtil{
  static SharedPreferences preferences;
  static PackageInfo packageInfo;
  static Future<bool> getInstance() async{
     preferences = await SharedPreferences.getInstance();
     packageInfo = await PackageInfo.fromPlatform();
     return true;
  }
}

3、在main方法里先调用初始化方法,再调用runApp方法

void main() {
  realRunApp();
}
void realRunApp() async {
  bool success = await SpUtil.getInstance();
  print("init-"+success.toString());
  runApp(MyApp());
} 

4、在需要使用时就可以同步调用了

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    Widget rootPage;
    String version = SpUtil.packageInfo.version;
    String oldVersion = SpUtil.preferences.getString('oldVersion');
    if(version == oldVersion){
      // 如果已经显示过了则直接显示tab
        rootPage = TabBarPage();
    }else{
      // 没有显示过guide,则优先显示guide
        rootPage = CircleGuidePage();
    }
    return MaterialApp(
      title: 'MyApp',
      debugShowCheckedModeBanner: true,
      home: rootPage,
    );
  }
}
01-23 07:31