Flutter使用easy_localization无法加载en.json资源的问题
问题:EasyLocalization无法加载翻译资源文件
我已在pubspec.yaml中正确声明资源,应用可正常运行,但模拟器提示Unable to load assets: "assets/translation/en.json",提示资源不存在或数据为空。然而打开en.json文件,能看到其中包含内容。我已附上pubspec.yaml配置截图、en.json文件截图及模拟器报错截图,以下是使用easy_localization包的main.dart和app.dart代码,寻求问题解决方法:
main.dart代码
import 'dart:io'; import 'package:easy_localization/easy_localization.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:hive/hive.dart'; import 'package:kimathilegal_app/app.dart'; import 'package:path_provider/path_provider.dart'; import 'models/constants.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await EasyLocalization.ensureInitialized(); await Firebase.initializeApp(); Directory directory = await getApplicationDocumentsDirectory(); Hive.init(directory.path); await Hive.openBox(Constants.bookmarkTag); await Hive.openBox(Constants.recentSearchTag); await Hive.openBox(Constants.notificationTag); SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle( statusBarColor: Colors.transparent, statusBarIconBrightness: Brightness.dark)); runApp( EasyLocalization( supportedLocales: const [Locale('en'), Locale('ar'), Locale('es')], path: 'assets/translation', fallbackLocale: const Locale('en'), //Default language startLocale: const Locale('en'), useOnlyLangCode: true, child: const MyApp(), ) ); }
app.dart代码
import 'package:firebase_analytics/firebase_analytics.dart'; import 'package:flutter/material.dart'; import 'package:kimathilegal_app/pages/splash_page.dart'; import 'package:provider/provider.dart'; import 'blocs/ads_bloc.dart'; import 'blocs/category_bloc.dart'; import 'blocs/category_tab1_bloc.dart'; import 'blocs/category_tab2_bloc.dart'; import 'blocs/category_tab3_bloc.dart'; import 'blocs/category_tab4_bloc.dart'; import 'blocs/featured_bloc.dart'; import 'blocs/latest_articles_bloc.dart'; // import 'blocs/notification_bloc.dart'; import 'blocs/popular_articles_bloc.dart'; import 'blocs/settings_bloc.dart'; import 'blocs/tab_index_bloc.dart'; import 'blocs/theme_bloc.dart'; import 'blocs/user_bloc.dart'; import 'package:easy_localization/easy_localization.dart'; import 'models/theme.dart'; final FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.instance; final FirebaseAnalyticsObserver firebaseObserver = FirebaseAnalyticsObserver(analytics: firebaseAnalytics); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return ChangeNotifierProvider<ThemeBloc>( create: (_) => ThemeBloc(), child: Consumer<ThemeBloc>( builder: (_, mode, child) { return MultiProvider( providers: [ ChangeNotifierProvider<SettingsBloc>(create: (context) => SettingsBloc()), ChangeNotifierProvider<CategoryBloc>(create: (context) => CategoryBloc()), ChangeNotifierProvider<FeaturedBloc>(create: (context) => FeaturedBloc()), ChangeNotifierProvider<LatestArticlesBloc>(create: (context) => LatestArticlesBloc()), ChangeNotifierProvider<UserBloc>(create: (context) => UserBloc()), // ChangeNotifierProvider<NotificationBloc>(create: (context) => NotificationBloc()), ChangeNotifierProvider<PopularArticlesBloc>(create: (context) => PopularArticlesBloc()), ChangeNotifierProvider<AdsBloc>(create: (context) => AdsBloc()), ChangeNotifierProvider<TabIndexBloc>(create: (context) => TabIndexBloc()), ChangeNotifierProvider<CategoryTab1Bloc>(create: (context) => CategoryTab1Bloc()), ChangeNotifierProvider<CategoryTab2Bloc>(create: (context) => CategoryTab2Bloc()), ChangeNotifierProvider<CategoryTab3Bloc>(create: (context) => CategoryTab3Bloc()), ChangeNotifierProvider<CategoryTab4Bloc>(create: (context) => CategoryTab4Bloc()), ], child: MaterialApp( debugShowCheckedModeBanner: false, supportedLocales: context.supportedLocales, localizationsDelegates: context.localizationDelegates, navigatorObservers: [firebaseObserver], locale: context.locale, theme: ThemeModel().lightTheme, darkTheme: ThemeModel().darkTheme, themeMode: mode.darkTheme == true ? ThemeMode.dark : ThemeMode.light, home: const SplashPage() ) ); }, ), ); } }
内容的提问来源于stack exchange,提问作者Fred_Wolfe
相关产品推荐
相关产品推荐

