我的Flutter应用中出现此错误:
在null上调用了getter'auth'。 接收者:null 尝试致电:auth
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:smartbin/services/provider.dart';
import 'package:auro_avatar/auro_avatar.dart';
class Profile extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Profile'),
centerTitle: true,
backgroundColor: Colors.green[800]),
backgroundColor: Colors.green[400],
body: Container(
padding: EdgeInsets.all(20),
child: Column(children: <Widget>[
InitialNameAvatar(
"",
circleAvatar: true,
),
SizedBox(
height: 20,
),
//提出了用于编辑的按钮
RaisedButton(
onPressed: () {},
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Text('Edit'),
new Icon(Icons.edit),
],
),
color: Colors.greenAccent,
),
//将来的构建器,用于从Firebase导入数据
FutureBuilder(
future: Provider.of(context).auth.getCurrentUID(),
builder: (context, snapshot){
if (snapshot.connectionState == ConnectionState.done)
{
return Text("${snapshot.hasData}");
}
},
),
//自定义窗口小部件以制作图标文字
buildProfile(
icon: Icons.person,
text: 'Name:',
subtext: '',
),
SizedBox(height: 30),
buildProfile(
icon: Icons.email,
text: "Email:",
subtext: '',
)
]),
));
}
}
//自定义小部件
class buildProfile extends StatelessWidget {
IconData icon;
String text;
String subtext;
buildProfile({this.text, this.icon, this.subtext});
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Icon(icon),
SizedBox(
width: 10,
),
Text(text,
style: TextStyle(
fontSize: 20,
fontFamily: 'Solway',
fontWeight: FontWeight.bold,
)),
SizedBox(
width: 10,
),
Text(subtext,
style: TextStyle(
fontSize: 20,
fontFamily: 'Solway',
fontWeight: FontWeight.bold,
)),
],
);
}
}
该错误来自将来的构建器,该认证称auth被调用为null。我希望你们能帮助我。谢谢。
修改 提供程序代码在这里。
import 'package:firebase_auth/firebase_auth.dart';
import 'package:smartbin/services/auth.dart';
import 'package:flutter/material.dart';
class Provider extends InheritedWidget {
final AuthService auth;
Provider({Key key, Widget child, this.auth}) : super(key: key, child: child);
@override
bool updateShouldNotify(InheritedWidget oldWidget) {
return true;
}
static Provider of(BuildContext context) =>
// ignore: deprecated_member_use
(context.dependOnInheritedWidgetOfExactType<Provider>());
}
答案 0 :(得分:0)
问题出在您的FutureBuilder
,您致电.auth
而没有提供者。
你做到了
Provider.of(context).auth.getCurrentUID()
,表示您未指定任何Provider
。您应该这样访问您的提供商
Provider.of<AuthProvider>(context).auth.getCurrentUID() //replace AuthProvider with your provider's name
答案 1 :(得分:0)
您必须指定提供商
Provider.of<AuthProvider>(context).auth.getCurrentUID()