设置标题时出现颤振本地化错误

时间:2020-01-06 16:12:32

标签: flutter localization

localizaed.dart

Foo.m

main.dart

import 'package:flutter/cupertino.dart';

class Localized {
  Localized(this.locale);

  final Locale locale;

  static Localized of(BuildContext context) => Localizations.of<Localized>(context, Localized);
  static Map<String, Map<String,String>> _v = {
    'en': {
      'title': 'hello world',
    },
    'ja': {
      'title': 'こんちは'
    }
  };
  String get title => _v[locale.languageCode]['title'];
}

class LocalizedDelagate extends LocalizationsDelegate<Localized> {
  const LocalizedDelagate();

  @override
  bool isSupported(Locale locale) => ['en','ja'].contains(locale.languageCode);
  @override
  Future<Localized> load(Locale locale) async => Localized(locale);
  @override
  bool shouldReload(LocalizationsDelegate old) => false;
}

我不明白为什么在屏幕上出现此错误消息。

import 'package:calculator/src/localization/localized.dart';
import 'package:flutter/material.dart';
import 'package:calculator/src/pages/HomePage.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() => runApp(StockCalcApp());

class StockCalcApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        const LocalizedDelagate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: [
        const Locale('en'),
        const Locale('ja'),
      ],
      onGenerateTitle: (BuildContext context) => Localized.of(context).title, // notthing problem
      theme: ThemeData(primarySwatch: Colors.pink),
      home: Scaffold(
        resizeToAvoidBottomPadding: false,
        appBar: AppBar(
          title: Text(Localized.of(context).title), // and same code but, error. when I comment this line then, nothing well.
          backgroundColor: Colors.pink[900],
          elevation: 0.0,
        ),
        body: StockHome(),
      ),
    );
  }
}

我只是在网上找到此示例。而且我认为..非常简单..我认为。 但严重的是,我不明白为什么要在android模拟器上显示此消息。

1 个答案:

答案 0 :(得分:2)

调用VARIANT varArray; ocx_instance.GetItemArr(real, &varArray); LPSAFEARRAY psa = varArrar.parray; LONG lLBound, lUBound; SafeArrayGetLBound(psa, 1, &lLBound); SafeArrayGetUBound(psa, 1, &lUBound); CStringArray arrItem; VARIANT *varArrayData; if (SUCCEEDED(SafeArrayAccessData(psa, (void**) &varArrayData))) { int nItemCnt = (lUBound - lLBound + 1); for (int i = 0; i < nItemCnt; i++) { CString strItem = varArrayData[i].bstrVal; arrItem.Add(strItem); } SafeArrayUnaccessData(psa); } 时,它将使用新的onGenerateTitle: (BuildContext context) => Localized.of(context).title,其中已经包含BuildContext,因此可以用LocalizedDelagate()进行调用。

当您在相同的Localized.of(context)方法中使用它时,您在创建build之前引用了context的实例 ,因此{{1} }不会返回任何内容。

您可以通过创建一个新的窗口小部件来避免此问题,该窗口小部件的LocalizedDelagate()方法中将更新Localized.of(context),并且可以访问BuildContext。 例如,创建一个名为build

的新窗口小部件。
Localized