必须在flutter中初始化不可为空的示例字段[“controller”]

falq053o  于 2023-02-09  发布在  Flutter
关注(0)|答案(3)|浏览(134)

日安!我是一个新的flutter想开始我的启动,但我被困在这个问题,使标签栏的例子...
因为这些问题,我几乎沮丧了一整天。
Dart Analysis在下面一直这么说......
必须在flutter中初始化不可为空的示例字段["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 {

  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();
  }

        }
ujv3wf0j

ujv3wf0j1#

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

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();
  }

}
rbpvctlc

rbpvctlc2#

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

late TabController controller;

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

h79rfbju

h79rfbju3#

看起来你在你的项目中使用了null safety。所以dart正在抱怨这一行。Dart希望它在创建后立即被初始化:

TabController controller;

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

late TabController controller;

相关问题