1

我用Android Studio创建了一个新的Flutter 项目。我正在使用null-safety,我在物理设备上运行应用程序,但代码生成不起作用,.dart_tool 文件夹中没有flutter_gen文件,只有flutter_build文件夹。

我按照Flutter 的官方国际化文档中的步骤进行操作

我没有使用Flutter Intl插件,因为它不支持null-safety(我知道您可以制作一个脚本以在每个生成的文件的顶部添加旧的 dart 版本作为解决方法)

颤振医生命令输出:

[✓] Flutter (Channel beta, 1.26.0-17.5.pre, on Microsoft Windows [Versión 10.0.19042.804], locale es-ES)
    • Flutter version 1.26.0-17.5.pre at <PATH>
    • Framework revision 1fe38dcb5f (3 days ago), 2021-02-10 16:25:47 -0800
    • Engine revision d4453f6018
    • Dart version 2.12.0 (build 2.12.0-259.9.beta)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at <PATH>
    • Platform android-30, build-tools 30.0.2
    • Java binary at: <PATH>
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[✓] Android Studio (version 4.1.0)
    • Android Studio at <PATH>
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[✓] VS Code (version 1.53.2)
    • VS Code at <PATH>
    • Flutter extension version 3.19.0

[✓] Connected device (1 available)
    • <DEVICE> • android-arm • Android 8.1.0 (API 27)

• No issues found!

/pubscpec.yaml:

name: example_app
description: A new Flutter application.
publish_to: 'none' 
version: 1.0.0+1

environment:
  sdk: ">=2.12.0-0 <3.0.0"
  
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.17.0-nullsafety.2
  cupertino_icons: ^1.0.1
  
dev_dependencies:
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter

flutter:
  uses-material-design: true
  generate: true

/lib/main.dart:

import 'package:example_app/ui/screen/home_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
// TODO: uncomment the line below after codegen
// import 'package:flutter_gen/gen_l10n/app_localizations.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage('Flutter Demo Home Page'),
      localizationsDelegates: [
        // ... app-specific localization delegate[s] here
        // TODO: uncomment the line below after codegen
        // AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', ''), // English, no country code
        const Locale('es', ''), // Spanish, no country code
      ],
    );
  }
}

/lib/l10n.yaml:

arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart

lib/l10n/app_en.arb

{
  "helloWorld": "Hello World!"
}

lib/l10n/app_es.arb

{
  "helloWorld": "Hola mundo!"
}
4

1 回答 1

3

这是我的错误,移动/lib/l10n.yaml//l10n.yaml/解决问题。

perqin回答。

于 2021-02-16T19:23:16.473 回答