错误:未为类“ BuildContext”定义方法“ dependOnInheritedWidgetOfExactType”

时间:2019-12-16 15:35:33

标签: flutter

每次尝试调试时,都会出现此错误,错误:未为类“ BuildContext”定义方法“ dependOnInheritedWidgetOfExactType”。 我尝试了很多方法来解决它找不到自己的解决方法。 这是代码。请有理想的人来解决这个问题

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:card_settings/card_settings.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}




class _LoginPageState extends State<LoginPage> {
String title = "Spheria";
  String author = "Cody Leet";
  String url = "http://www.codyleet.com/spheria";

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  static LoginPage of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<LoginPage>();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
    body:  Form(
        key: _formKey,
        child: CardSettings(
          children: <Widget>[
            CardSettingsHeader(label: 'Favorite Book'),
            CardSettingsText(
              label: 'Title',
              initialValue: title,
              validator: (value) {
                if (value == null || value.isEmpty) return 'Title is required.';
              },
              onSaved: (value) => title = value,
            ),
            CardSettingsText(
              label: 'URL',
              initialValue: url,
              validator: (value) {
                if (!value.startsWith('http:')) return 'Must be a valid website.';
              },
              onSaved: (value) => url = value,
            ),
          ],
        ),
    ), 
    );
  }
}
    **Used it in this here
    Pls ignore extra brackets**

    import 'package:domesms/screens/Login.dart';
    import 'package:flutter/material.dart';

    class MyHomePage extends StatefulWidget {
    final String title ;

    MyHomePage({Key key, this.title}) : super(key: key);

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

    class _MyHomePageState extends State {
    @override
    Widget build(BuildContext context) {

        return Scaffold(
          appBar: AppBar(title: Text('DOMESMS.COM')),
          body: Center(child: Text('My Page!')),
          drawer: Drawer(
            // Add a ListView to the drawer. This ensures the user can scroll
            // through the options in the drawer if there isn't enough vertical
            // space to fit everything.
            child: ListView(
              // Important: Remove any padding from the ListView.
              padding: EdgeInsets.zero,
              children: <Widget>[
                UserAccountsDrawerHeader(
                accountName:  Text('Oludiya Ay`enter code here`odeji'),
                 accountEmail:Text('ayodiya@gmail.com'),
                 currentAccountPicture: CircleAvatar(
                   backgroundImage: AssetImage('images/unisexavatar.png',),
                   backgroundColor: Colors.white,
                   radius: 80
                 ),
              decoration: BoxDecoration(
                color: Colors.blue,
              ),
            ),
            ListTile(
              leading: Icon(Icons.person,
              color: Colors.cyanAccent),
              title: Text('ACCOUNT DETAILS'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer

                Navigator.pop(context);
                Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) => LoginPage()));

              },
            )

1 个答案:

答案 0 :(得分:0)

有点晚了,但是无论谁提出这个问题,希望这会有所帮助。 升级到 flutter 2.0 后,我遇到了同样的问题,

以下步骤解决了我的问题。

为了解决上面的错误,

  1. 打开你的 pubspec.yaml,
  2. 点击酒吧升级。

让它完成。然后再次运行您的项目。 错误得到解决。

see this image.