Flutter中的本地化字符串

时间:2019-01-16 04:03:43

标签: localization flutter widget

我正在构建一个演示应用程序,以使用本地化字符串进行测试。 我收到以下错误:

I / flutter(21588):在构建MainApp(dirty)时引发了以下NoSuchMethodError: I / flutter(21588):将getter'title'调用为null。 I / flutter(21588):接收者:null I / flutter(21588):尝试致电:标题

我不确定为什么会出现此错误。我遵循了Flutter文档中的指示。

我有以下本地化课程:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'dart:async';
import 'package:bet_master/l10n/messages_all.dart';

class AppLocalizations {
  static Future<AppLocalizations> load(Locale locale) {
    final String name =
        locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
    final localeName = Intl.canonicalizedLocale(name);

    return initializeMessages(localeName).then((bool _) {
      Intl.defaultLocale = localeName;
      return AppLocalizations();
    });
  }
  static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations);
  }

  String get title {
    return Intl.message(
      'Bet Master',
      name: 'title',
      desc: 'App Title'
    );
  }

  String get search {
    return Intl.message(
      'Search',
      name: 'search',
      desc : ''
    );
  }

}

class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
  const AppLocalizationsDelegate();

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

  @override
  Future<AppLocalizations> load(Locale locale) {
    return AppLocalizations.load(locale);
  }

  @override
  bool shouldReload(AppLocalizationsDelegate old) {
    return false;
  }
}

对于Home小部件,我仅设置标题

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:bet_master/localization/localizations.dart';

void main() {
  runApp(MainApp());
}

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
        localizationsDelegates: [
          const AppLocalizationsDelegate(),
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        supportedLocales: [
          const Locale('en', ''),
          const Locale('es', ''),
          const Locale('fr', ''),
        ],
        home: Scaffold(
          appBar: AppBar(
            title: Text(AppLocalizations.of(context).title),
          ),
        ),
      );
  }
}

2 个答案:

答案 0 :(得分:1)

最后,问题似乎与本地化不可用有关,我将“ home”代码移至另一个小部件并解决了该问题。

Widget build(BuildContext context) {
    return new MaterialApp(
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          _appLocalizationsDelegate,
        ],
        supportedLocales: [
           Locale('en'),
           Locale('es'),
        ],
        locale: _appLocalizationsDelegate.overridenLocale,
        onGenerateTitle: (BuildContext context) => AppLocalizations.of(context).title,
        home: Home()
      );
  }
}

class Home extends StatelessWidget {
  @override
    Widget build(BuildContext context) {
      return new Scaffold(
          appBar: AppBar(
            title: Text(AppLocalizations.of(context).title),
          ),
          body: Column(children: <Widget>[
            Text('hola'),
          ],) 
        );
    }
}

我仍然需要研究为什么需要这样做,但至少现在可以了。

答案 1 :(得分:0)

我有同样的错误,但是我的问题是我使用了两个嵌套 MaterialApp()小部件。

这导致该行重新调整为null:

__global__ void Mult(float* devPtrA, float* devPtrB, float* devPtrC, size_t pitch, int Ncols, int Nrows, int n)
{
    float temp;

    int r = blockDim.y * blockIdx.y + threadIdx.y;      //for (int f = 0; f <= fil - 1; f += 1) equivalencia en for
    int c = blockDim.x * blockIdx.x + threadIdx.x;      //for (int c = 0; c <= col - 1; c += 1)
    if ((r < Ncols) && (c < Nrows)) {
        for (int c2 = 0; c2 < n; c2++) {
            float* vertical = (float*)((char*)devPtrA + r * pitch);
            float element1 = vertical[c2];
            float* horizontal = (float*)((char*)devPtrB + c2 * pitch);
            float element2 = horizontal[c];
            temp += element1 * element2;
        }


        //printf("\nla fila es: %d la columna es: %d el valor es: %8.4f\n\n", r, c, temp);
        float* vertical2 = (float*)((char*)devPtrC + r * pitch);
        vertical2[c] = temp;
    }
}

我用一个脚手架替换了嵌套的MarerialApp(child)小部件,它可以工作了。

希望它可以帮助某人!