首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >flutter:未处理异常:无法加载资产: assets/languages/en

flutter:未处理异常:无法加载资产: assets/languages/en
EN

Stack Overflow用户
提问于 2020-07-06 00:54:02
回答 1查看 2.3K关注 0票数 5

我正在使用flutter本地化,并且我添加了我的语言和参数,以便翻译为每种语言的json文件。语言json文件位于根文件夹中名为asset的文件夹中,如下所示

我还在pubspec.yaml中声明了它,如下所示

下面是我的en json代码:

代码语言:javascript
复制
{
  "home_title": "Welcome To Tamata! \nThe online supermarket"
}

下面是我的material应用程序:

代码语言:javascript
复制
MaterialApp(
        locale: _locale,
        supportedLocales: [
          Locale('en', 'US'),
          Locale('ar', ''),
//          Locale('ar', 'IQ'),
        ],
        localizationsDelegates: [
          DemoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        localeResolutionCallback: (deviceLocale, supportedLocales) {
          for (var locale in supportedLocales) {
            if (locale.languageCode == deviceLocale.languageCode &&
                locale.countryCode == deviceLocale.countryCode) {
              return deviceLocale;
            }
          }
          return supportedLocales.first;
        },
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            primaryColor: Color(0xffba0100),
            accentColor: Color(0xff188949),
            canvasColor: Colors.grey[100],
            textTheme: TextTheme().copyWith(
              bodyText1: TextStyle(
                color: Colors.white,
                fontSize: 17.0,
                fontWeight: FontWeight.w700,
              ),
              bodyText2: TextStyle(
                color: Colors.white,
              ),
              headline6: TextStyle(
                color: Colors.black,
                fontSize: 18.0,
              ),
            )),
        title: 'Tamata Online',
        initialRoute: '/',
        routes: {
          '/': (ctx) => LoadingScreen(
              initScreen), //TODO put it back to be LoadingScreen(initScreen)
          TabsScreen.id: (ctx) => TabsScreen(
                filteredBySearch: filteredBySearch,
                filteredBySpecialSearch: filteredBySpecialSearch,
              ),
          SettingsScreen.id: (ctx) => SettingsScreen(),
          CartScreen.id: (ctx) => CartScreen(),
          IntroScreen.id: (ctx) => IntroScreen(),
          ChooseLanguageScreen.id: (ctx) => ChooseLanguageScreen(),
          SpecialOffers.id: (ctx) => SpecialOffers(),
        },
      ),

下面是我的本地化演示:

代码语言:javascript
复制
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class DemoLocalizations {
  final Locale locale;

  DemoLocalizations(this.locale);

  static DemoLocalizations of(BuildContext context) {
    return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
  }

  Map<String, String> _localizedValues;

  Future load() async {
    String jsonStringValues =
        await rootBundle.loadString('assets/languages/${locale.languageCode}'); //where it says it has a problem reading my assets/language/en

    Map<String, dynamic> mappedJson = jsonDecode(jsonStringValues);
    _localizedValues =
        mappedJson.map((key, value) => MapEntry(key, value.toString()));
  }

  String getTranslatedValue(String key) {
    return _localizedValues[key];
  }

  static const LocalizationsDelegate<DemoLocalizations> delegate =
      _DemoLocalizationDelegate();
}

class _DemoLocalizationDelegate
    extends LocalizationsDelegate<DemoLocalizations> {
  const _DemoLocalizationDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'ar'].contains(locale.languageCode);
  }

  @override
  Future<DemoLocalizations> load(Locale locale) async {
    DemoLocalizations localization = DemoLocalizations(locale);
    await localization.load();
    return localization;
  }

  @override
  bool shouldReload(_DemoLocalizationDelegate old) => false;
}

我做错了什么?

下面是错误:

代码语言:javascript
复制
Unable to load asset: assets/languages/en
E/flutter (19559): #0      PlatformAssetBundle.load
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-06 01:03:41

因为您的本地化文件是JSON格式的,所以您需要在文件路径中添加.json。

代码语言:javascript
复制
Future load() async {
    String jsonStringValues =
        await rootBundle.loadString('assets/languages/${locale.languageCode}.json'); // add .json at the end

    Map<String, dynamic> mappedJson = jsonDecode(jsonStringValues);
    _localizedValues =
        mappedJson.map((key, value) => MapEntry(key, value.toString()));
  }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62743682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档