是否可以使用bloc在void Main方法上存储数据?

时间:2019-09-10 11:25:08

标签: flutter dart sqflite

我使用了本地化。我选择语言,我使用sqflite存储数据。默认情况下,我获取设备语言代码并需要存储它。我用过集团。

我的MaterialApp看起来像这样,

MyApp class{

    return MaterialApp(
      localizationsDelegates: [
        _newLocaleDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [const Locale("en", ""), const Locale("es", "")],
      home: BlocProvider(
        bloc: LanguagesBloc(),
        child: LoginScreen(),
      ),
    );

initState和方法

  @override
  void initState() {
    super.initState();
    _newLocaleDelegate = AppTranslationsDelegate(
        newLocale: Locale(ui.window.locale.languageCode));
    application.onLocaleChanged = onLocaleChange;
  }

  void onLocaleChange(Locale locale) {
    setState(() {
      _newLocaleDelegate = AppTranslationsDelegate(newLocale: locale);
    });
  }

}//end MyApp class

我在LoginScreen上选择了语言数据

_languagesBloc.inAddLanguage.add(Language(language: value,id: id));

dao类如下

  newLanguage(Language language) async {
    final db = await database;
    var res = await db.insert('language', language.toJson());

    return res;
  }

  getLanguages() async {
    final db = await database;
    var res = await db.query('language');
    List<Language> languages = res.isNotEmpty ? res.map((note) => Language.fromJson(note)).toList() : [];

    return languages;
  }

是否可以使用bloc在void Main方法上存储数据?

0 个答案:

没有答案