我无法导入initState

问题描述 投票:-3回答:2

每次我在State类中导入initState时都会出现此错误

类'IntroViewState'不能定义方法'State'并且具有相同名称的字段'initState.State'。

这是我用来调用initState的代码

import 'package:flutter/material.dart';
import 'package:idb/pages/adminpage.dart';
import 'package:idb/pages/homepage.dart';
import 'package:intro_views_flutter/Models/page_view_model.dart';
import 'package:intro_views_flutter/intro_views_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';

class IntroView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return IntroViewState();
  }
}

class IntroViewState extends State<IntroView> {
  BuildContext context;

  Future checkFirstSeen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool _seen = (prefs.getBool('seen') ?? true);

    setState(() {
      if (_seen) {
        Navigator.of(context).pushReplacement(
            new MaterialPageRoute(builder: (context) => new AdminPage()));
      } else {
        prefs.setBool('seen', true);

        Navigator.of(context).pushReplacement(
            new MaterialPageRoute(builder: (context) => new IntroView()));
      }
    });
  }

  final pages = [
    PageViewModel(
        pageColor: Colors.white,
        bubble: Image.asset('assets/t.jpg'),
        body: Text('with the app you can check the card validation'),
        title: Text('Card Validation'),
        textStyle: TextStyle(
          color: Colors.tealAccent,
        ),
        mainImage: Image.asset(
          'assets/check.jpg',
          height: 500.0,
          width: 500.0,
          alignment: Alignment.center,
        )),

  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.orange,
        primarySwatch: Colors.orange,
        accentColor: Colors.orangeAccent,
      ),
      debugShowCheckedModeBanner: false,
      home: Builder(
        builder: (context) => IntroViewsFlutter(
              pages,
              onTapDoneButton: () {
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => AdminPage()),
                );
              },
              onTapSkipButton: () {
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => AdminPage()),
                );
              },
              pageButtonTextStyles:
                  TextStyle(color: Colors.black, fontSize: 18.0),
            ),
      ),
    );
  }

  @override
  void initState {
    super.initState;
    checkFirstSeen();
  }
}

为什么?我怎么解决呢?而出现的另一个错误是

NoSuchMethodError:在null上调用方法'ancestorStateOfType'。 E / flutter(28147):接收者:null E / flutter(28147):尝试调用:ancestorStateOfType('TypeMatcher'实例)E / flutter(28147):#0 Object.noSuchMethod(dart:core / runtime / libobject_patch.dart:50:5)E / flutter(28147):#1 Navigator.of(package:flutter / src / widgets / navigator.dart:1288:19)E / flutter (28147):#2 IntroViewState.checkFirstSeen。 (包:IDB /页/ introview.dart:31:15)

dart flutter
2个回答
0
投票

删除context,我认为它正在创建null问题

class IntroViewState extends State<IntroView> {


  Future checkFirstSeen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool _seen = (prefs.getBool('seen') ?? true);


      if (_seen) {
        Navigator.of(context).pushReplacement(
            new MaterialPageRoute(builder: (context) => new AdminPage()));
      } else {
        prefs.setBool('seen', true);

        Navigator.of(context).pushReplacement(
            new MaterialPageRoute(builder: (context) => new IntroView()));
      }

  }

  final pages = [
    PageViewModel(
        pageColor: Colors.white,
        bubble: Image.asset('assets/t.jpg'),
        body: Text('with the app you can check the card validation'),
        title: Text('Card Validation'),
        textStyle: TextStyle(
          color: Colors.tealAccent,
        ),
        mainImage: Image.asset(
          'assets/check.jpg',
          height: 500.0,
          width: 500.0,
          alignment: Alignment.center,
        )),

  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.orange,
        primarySwatch: Colors.orange,
        accentColor: Colors.orangeAccent,
      ),
      debugShowCheckedModeBanner: false,
      home: Builder(
        builder: (context) => IntroViewsFlutter(
              pages,
              onTapDoneButton: () {
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => AdminPage()),
                );
              },
              onTapSkipButton: () {
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => AdminPage()),
                );
              },
              pageButtonTextStyles:
                  TextStyle(color: Colors.black, fontSize: 18.0),
            ),
      ),
    );
  }

  @override
  void initState {
    super.initState();
    checkFirstSeen();
  }
}

0
投票

这是工作代码,你犯了很多错误,我修改了它。随意根据您的需求进行更改。

void main() => runApp(MaterialApp(home: IntroView(),));

class IntroView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return IntroViewState();
  }
}

class IntroViewState extends State<IntroView> {
  @override
  void initState() {
    super.initState();
    Timer.run(() => checkFirstSeen());
  }

  Future checkFirstSeen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool _seen = (prefs.getBool('seen') ?? true);

    if (_seen)
      _navigateToAdminPage();
    else
      prefs.setBool('seen', true);
  }

  void _navigateToAdminPage() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) => AdminPage()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("App"),),
      body: Column(
        children: <Widget>[
          RaisedButton(
            child: Text("Done"),
            onPressed: _navigateToAdminPage,
          ),
          RaisedButton(
            child: Text("Skip"),
            onPressed: _navigateToAdminPage,
          ),
        ],
      ),
    );
  }
}

class AdminPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Admin Page")),
      body: Center(child: Text("This is admin page"),),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.