【问题标题】:why is builder in flutter breaking here为什么颤振中的建造者在这里打破
【发布时间】:2020-06-08 15:34:33
【问题描述】:

我今天已经修复了我的第一组错误,但是现在在这一行代码中断了,我无法弄清楚

这是有问题的代码行

builder: (BuildContext context) {... }

这里是错误:

编译器消息: lib/main.dart:11:5:错误:没有名为“builder”的命名参数。 构建器:(BuildContext 上下文){ ^^^^^^^ ../../Downloads/flutter_windows_v1.12.13+hotfix.8-stable/src/flutter/.pub-cache/hosted/pub.dartlang.org/provider-4.0.4/lib/src/change_notifier_provider.dart:107 :3: 上下文:找到了这个候选人,但参数不匹配。 ChangeNotifierProvider({ ^^^^^^^^^^^^^^^^^^^^^^ 目标 kernel_snapshot 失败:异常:快照创建期间出错:null 构建失败。

FAILURE:构建失败并出现异常。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'home_page.dart';
import 'auth.dart';
import 'login_page.dart';

void main() => runApp(
  ChangeNotifierProvider<AuthService>(
    child: MyApp(),
    builder: (BuildContext context) {
      return AuthService();
    },
  ),
);

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: FutureBuilder<FirebaseUser>(
        future: Provider.of<AuthService>(context).getUser(),
        builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            // log error to console
            if (snapshot.error != null) {
              print("error");
              return Text(snapshot.error.toString());
            }

            // redirect to the proper page
            return snapshot.hasData ? HomePage(snapshot.data) : LoginPage();
          } else {
            // show loading indicator
            return LoadingCircle();
          }
        },
      ),
    );
  }
}

class LoadingCircle extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: CircularProgressIndicator(),
        alignment: Alignment(0.0, 0.0),
      ),
    );
  }

【问题讨论】:

    标签: android ios flutter dart mobile


    【解决方案1】:

    ChangeNotifierProvider 没有 builder 属性。你想使用create 属性:

    ChangeNotifierProvider<AuthService>(
      create: (context) => AuthService(),
      child: MyApp(),
    },
    

    (documentation)

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      相关资源
      最近更新 更多