不可为空的实例字段 ['controller'] 必须在 flutter 中初始化

Non-nullable instance field ['controller'] must be initialized in flutter

美好的一天! 我是 Flutter 的新手,想开始我的 Startup,但我被困在这个问题中以制作 Tabbar 示例...

因为这个问题我几乎郁闷了一整天....

Dart Analysis 在下面一直这么说....

不可为空的实例字段['controller']必须在flutter中初始化

非常感谢。

        import 'package:flutter/material.dart';
import 'sub/firstPage.dart';
import 'sub/secondPage.dart';


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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);



  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {

  TabController controller;

  @override
  void initState() {
    super.initState();
    controller = TabController(length: 2, vsync: this);


  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('TabBar Example'),
        ),
        body: TabBarView(
          children: <Widget>[FirstApp(), SecondApp()],
          controller: controller,
        ),
        bottomNavigationBar: TabBar(tabs: [
          Tab(icon: Icon(Icons.looks_one, color: Colors.blue),),
          Tab(icon: Icon(Icons.looks_two, color: Colors.blue),),
        ],
        )
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }


        }

由于您的字段不允许空值,但您无法在创建字段时对其进行初始化,因此您必须将其声明为 late:

late TabController controller;

这允许您稍后分配一个值,但是如果您在分配之前使用它,您得到一个异常。你的代码看起来应该没问题。

请尝试,你错过了使用控制器

import 'package:flutter/material.dart';
import 'sub/firstPage.dart';
import 'sub/secondPage.dart';


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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);



  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {

  late TabController controller;

  @override
  void initState() {
    super.initState();
    controller = TabController(length: 2, vsync: this);


  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('TabBar Example'),
        ),
        body: TabBarView(
          children: <Widget>[FirstApp(), SecondApp()],
          controller: controller,
        ),
        bottomNavigationBar: TabBar(
          controller: controller, // add here 
          tabs: [
          Tab(icon: Icon(Icons.looks_one, color: Colors.blue),),
          Tab(icon: Icon(Icons.looks_two, color: Colors.blue),),
        ],
        )
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }


}


您似乎在项目中使用了空安全。所以飞镖抱怨这条线。 Dart 期望它在创建后立即初始化:

TabController controller;

要解决此问题,请在 TabController 之前添加“late”关键字:

late TabController controller;