Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 是否可以使用bloc在void Main方法上存储数据?_Flutter_Dart_Sqflite - Fatal编程技术网

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

Flutter 是否可以使用bloc在void Main方法上存储数据?,flutter,dart,sqflite,Flutter,Dart,Sqflite,我使用本地化。I用户选择语言I使用sqflite存储数据。默认情况下,我获取设备语言代码并需要存储它。我使用了bloc 我的材料是这样的 MyApp class{ return MaterialApp( localizationsDelegates: [ _newLocaleDelegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.de

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

我的材料是这样的

MyApp class{

    return MaterialApp(
      localizationsDelegates: [
        _newLocaleDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [const Locale("en", ""), const Locale("es", "")],
      home: BlocProvider(
        bloc: LanguagesBloc(),
        child: LoginScreen(),
      ),
    );
初始化状态和方法

  @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
I登录屏幕上的用户选择的语言数据

_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;
  }
newLanguage(语言)异步{
最终数据库=等待数据库;
var res=await db.insert('language',language.toJson());
返回res;
}
getLanguages()异步{
最终数据库=等待数据库;
var res=await db.query('language');
List languages=res.isNotEmpty?res.map((注意)=>Language.fromJson(注意)).toList():[];
返回语言;
}

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

也许这不是更好的方法,但您可以尝试将语言代码存储到共享首选项中。可以使用语言代码开关构建您的LanguagesBloc,并将共享首选项下的语言代码分派到LanguagesBloc。我已经使用共享首选项来获取用户记录的详细信息