Флаттер Интернационализация IOS - PullRequest
0 голосов
/ 17 февраля 2020

Я пытаюсь запустить интернационализацию Flutter впервые - я получаю сообщение об ошибке ниже. Кто-нибудь может мне помочь, почему перевод вызывается на нуль и получает ноль?

Метод 'translate' был вызван на ноль. Получатель: null Пробный вызов: translate ("menu")

Мой файл меню выглядит следующим образом:

import 'package:debitor2_app/services/app_localizations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';


class forside extends StatefulWidget {
  @override
  _forsideState createState() => _forsideState();
}

class _forsideState extends State<forside> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      supportedLocales: [
        Locale('en', 'US'),
        Locale('dk', 'DK'),
      ],

        localizationsDelegates: [

          AppLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,

        ],
        //var locale.countryCode = uk,



        localeResolutionCallback: (locale, supportedLocales) {
          if (locale == null) {
            return supportedLocales.first;
          }
        for (var supportedLocales in supportedLocales ) {
          if(supportedLocales.languageCode == locale.languageCode &&
              supportedLocales.countryCode == locale.countryCode) {
            return supportedLocales;
          }


          //if (locale == null) {
            //return supportedLocales.first;
          //}
        }
        print(locale);
        print(locale.languageCode);
        print(locale.countryCode);
        //return supportedLocales.first;
          return supportedLocales.first;
          //if(locale == null) {
            //locale = Localizations.localeOf(context);
          //}

        },
        home: Scaffold(
          appBar: AppBar(
            title: Text('Brew App'),
            backgroundColor: Colors.green[800],

      ),
      backgroundColor: Colors.green[100],



      //these delegates make sure that the localization data for the proper language



      body: Container(
        padding: EdgeInsets.all(30.0),
        child: GridView.count(
          crossAxisCount: 2,
          children: <Widget>[
            Card(
                margin: EdgeInsets.all(8.0),
                child: InkWell(
                  onTap: (){},
                  splashColor: Colors.green,
                  child: Center(
                    child: Column(

                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Icon(Icons.add_box, size: 70.0, color: Colors.orange,),
                        Text("Calculator", style: TextStyle(fontSize: 17.0)),
                      ],
                    ),
                  ),
                )

            ),
            Card(
                margin: EdgeInsets.all(8.0),
                child: InkWell(
                  onTap: (){},
                  splashColor: Colors.green,
                  child: Center(
                    child: Column(

                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Icon(Icons.local_library, size: 70.0, color: Colors.blue,),
                        Text(
                            //"Knowledge",

                            AppLocalizations.of(context).translate("menu"),
                            style: TextStyle(fontSize: 17.0)),
                      ],
                    ),
                  ),
                )

            ),


          ],

        ),
      ),
        ),
    );
  }
}

А мои app_localizations выглядят так:

import 'package:debitor2_app/services/app_localizations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';


class forside extends StatefulWidget {
  @override
  _forsideState createState() => _forsideState();
}

class _forsideState extends State<forside> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      supportedLocales: [
        Locale('en', 'US'),
        Locale('dk', 'DK'),
      ],

        localizationsDelegates: [

          AppLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,

        ],
        //var locale.countryCode = uk,



        localeResolutionCallback: (locale, supportedLocales) {
          if (locale == null) {
            return supportedLocales.first;
          }
        for (var supportedLocales in supportedLocales ) {
          if(supportedLocales.languageCode == locale.languageCode &&
              supportedLocales.countryCode == locale.countryCode) {
            return supportedLocales;
          }


          //if (locale == null) {
            //return supportedLocales.first;
          //}
        }
        print(locale);
        print(locale.languageCode);
        print(locale.countryCode);
        //return supportedLocales.first;
          return supportedLocales.first;
          //if(locale == null) {
            //locale = Localizations.localeOf(context);
          //}

        },
        home: Scaffold(
          appBar: AppBar(
            title: Text('Brew App'),
            backgroundColor: Colors.green[800],

      ),
      backgroundColor: Colors.green[100],



      //these delegates make sure that the localization data for the proper language



      body: Container(
        padding: EdgeInsets.all(30.0),
        child: GridView.count(
          crossAxisCount: 2,
          children: <Widget>[
            Card(
                margin: EdgeInsets.all(8.0),
                child: InkWell(
                  onTap: (){},
                  splashColor: Colors.green,
                  child: Center(
                    child: Column(

                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Icon(Icons.add_box, size: 70.0, color: Colors.orange,),
                        Text("Calculator", style: TextStyle(fontSize: 17.0)),
                      ],
                    ),
                  ),
                )

            ),
            Card(
                margin: EdgeInsets.all(8.0),
                child: InkWell(
                  onTap: (){},
                  splashColor: Colors.green,
                  child: Center(
                    child: Column(

                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Icon(Icons.local_library, size: 70.0, color: Colors.blue,),
                        Text(
                            //"Knowledge",

                            AppLocalizations.of(context).translate("menu"),
                            style: TextStyle(fontSize: 17.0)),
                      ],
                    ),
                  ),
                )

            ),


          ],

        ),
      ),
        ),
    );
  }
}
...