我无法导入initState

时间:2019-04-11 12:37:02

标签: dart flutter

每次我在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:调用了方法'ancestorStateOfType'   空值。 E / flutter(28147):接收方:空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   (包:flutter / src / widgets / navigator.dart:1288:19)E / flutter   (28147):#2 IntroViewState.checkFirstSeen。   (package:idb / pages / introview.dart:31:15)

2 个答案:

答案 0 :(得分: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"),),
    );
  }
}

答案 1 :(得分:0)

删除context,我认为这是在创建空问题

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