从数据库中获取数据时如何显示指示符

时间:2020-04-21 07:15:29

标签: flutter flutter-layout sqflite

我正在尝试在应用启动时打开数据库。当我尝试从数据库读取数据时,大约需要1秒钟或更长时间,并且由于read方法是Future,因此应用程序会在初始化数据库之前加载主窗口小部件树,并且由于fetch方法尚未完成加载它返回NULL。在应用启动时还有其他方法可以加载数据库吗?这是我的代码:

void main() => runApp(MaterialApp(
  home: MyApp(),
));

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
FlutterStatusbarcolor.setStatusBarColor(Colors.grey);


List<int> settings = [];

getSettings() async {

  try {
    List<SettingsDB> settingsGetter = await getData('settings', 'SettingsDB');
    SettingsDB settingsDBInstance = settingsGetter[0];
    print(settingsGetter);
    settings.add(settingsDBInstance.darkThemeEnabled);
    settings.add(settingsDBInstance.freeExamsProgress);
    settings.add(settingsDBInstance.mainExamsProgress);
    settings.add(settingsDBInstance.signsExamsProgress);
    settings.add(settingsDBInstance.mixedExamsProgress);
  }catch (e) {
    print(e);
  }
}
getSettings();

return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'title',
  home: MainPage(settings: settings,),
);
}
}

class MainPage extends StatefulWidget {

final List<int> settings;

const MainPage({Key key, this.settings}) : super(key: key);

@override
_MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {

@override
Widget build(BuildContext context) {



return Scaffold(
  backgroundColor: Color(0xffE0E0E0),
  body:

  SingleChildScrollView(
    physics: BouncingScrollPhysics(),
    child: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[

          SizedBox(height: 36.0,),

          Container(
            width: double.infinity,
            height: double.infinity,
            child: Text(widget.settings[0].toString()),
          ),

          Container(
            width: double.infinity,
            height: double.infinity,
            child: Text(widget.settings[1].toString()),
          ),

          Container(
            width: double.infinity,
            height: double.infinity,
            child: Text(widget.settings[2].toString()),
          ),

          Container
            width: double.infinity,
            height: double.infinity,
            child: Text(widget.settings[3].toString()),
          ),
        ],
      ),
    ),
  ),
);
}
}

2 个答案:

答案 0 :(得分:0)

如果在加载该数据之前无法向用户显示任何有意义的信息,则可以通过使您的主要方法异步来调用runApp之前做同样的事情。

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final data = await getData(); // whatever is needed to load your data
  runApp(MyApp());
}

答案 1 :(得分:0)

您可以使用如下所示的FutureBuilder小部件:

                       FutureBuilder(
                          future: widget._bloc.getAllPartners(),
                          builder: (BuildContext context,
                              AsyncSnapshot<List<UserPartner>> snapshot) {
                            if (snapshot.hasData) {
                              return Flexible(
                                child: _getSortByPartnerDropDown(snapshot.data),
                              );
                            } else {
                              return Flexible(
                                child: CircularProgressIndicator(
                                  valueColor: AlwaysStoppedAnimation<Color>(
                                      IntimassyColor.primaryTextWhiteHint),
                                ),
                              );
                            }
                          },
                        ),