【问题标题】:Implementing provider实施提供者
【发布时间】:2020-01-15 22:35:16
【问题描述】:

我正在尝试在我的应用程序顶部实现提供程序,但出现错误:“位置参数:预期为 0,但找到了 1”。我尝试了不同的实现,但随后出现运行时错误“构建函数返回 null”。下面的实现有问题吗?

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Provider<UserModel>(
      builder: (context) => UserModel(),
      return new MaterialApp(
        title: 'Profile Demo',
        theme: new ThemeData(
          primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Profile'),
    ),
    ),
    );
  }
}

我的 UserModel 如下所示:

class UserModel extends ChangeNotifier {

  String uid;
}

完整的 MyHomePage 代码:

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

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final _width = MediaQuery.of(context).size.width;
    final _height = MediaQuery.of(context).size.height;
    return StreamBuilder<DocumentSnapshot>(
        stream: Firestore.instance
            .collection(Provider.of<UserModel>(context).uid)
            .document('testuser')
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Stack(
              children: <Widget>[
                new Container(
                  color: Colors.blue,
                ),
                new Image.network(
                  snapshot.data['photourl'].toString(),
                  fit: BoxFit.fill,
                ),
                new BackdropFilter(
                    filter: new ui.ImageFilter.blur(
                      sigmaX: 6.0,
                      sigmaY: 6.0,
                    ),
                    child: new Container(
                      decoration: BoxDecoration(
                        color: Colors.blue.withOpacity(0.9),
                        borderRadius: BorderRadius.all(Radius.circular(50.0)),
                      ),
                    )),
                new Scaffold(
                    appBar: new AppBar(
                      title: new Text(widget.title),
                      centerTitle: false,
                      elevation: 0.0,
                      backgroundColor: Colors.transparent,
                    ),
                    drawer: new Drawer(
                      child: new Container(),
                    ),
                    backgroundColor: Colors.transparent,
                    body: new Center(
                      child: new Column(
                        children: <Widget>[
                          new SizedBox(
                            height: _height / 12,
                          ),
                          new CircleAvatar(
                            radius: _width < _height ? _width / 4 : _height / 4,
                            backgroundImage: NetworkImage(snapshot.data['photourl']),
                          ),
                          new SizedBox(
                            height: _height / 25.0,
                          ),
                          new Text(
                            snapshot.data['name'],
                            style: new TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: _width / 15,
                                color: Colors.white),
                          ),
                          new Padding(
                            padding: new EdgeInsets.only(
                                top: _height / 30,
                                left: _width / 8,
                                right: _width / 8),
                          ),
                          new Divider(
                            height: _height / 15,
                            color: Colors.white,
                          ),
                          new Row(
                            children: <Widget>[
                              rowCell(
                                  snapshot.data['totalquestions'], 'Answers'),
                              rowCell(
                                  '£ ${int.parse(snapshot.data['totalquestions']) * 2}', 'Earned'),
                            ],
                          ),
                          new Divider(
                              height: _height / 15, color: Colors.white),
                        ],
                      ),
                    ))
              ],
            );
          } else {
            return CircularProgressIndicator();
          }
        });
  }

【问题讨论】:

  • 你能发布new MyHomePage(title: 'Profile')的代码吗?必须有一个不需要的额外参数。
  • @VrushiPatel 当然,刚刚添加在上面
  • 首先,我在您的模型中看不到 notifyListeners()。其次,当您将鼠标悬停在有问题的构造函数上时,“位置参数:预期为 0,但找到了 1”应该在 IDE 中显示错误。哪一个? --- 您通常可以通过查找在 * 之后具有 anything 且不是命名参数的构造函数来查找位置参数错误。

标签: flutter flutter-provider


【解决方案1】:

问题是您正在尝试返回 MaterialApp。您需要将 MaterialApp 设置为 Provider 的子项,而不是返回它:

Provider<UserModel>(
builder: (context) => UserModel(),
child: MaterialApp(      // <<<<<<<<<<<<<<<<<<<<<<<<< Here
title: 'Profile Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),

【讨论】:

    猜你喜欢
    • 2011-01-19
    • 2014-10-27
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2013-11-03
    相关资源
    最近更新 更多