颤振提供者 "uid was caled on null"
Flutter Provider "uid was caled on null"
在过去的几个月里,我一直在研究 Flutter,该死的,我被困在了这个上。
我想设置一个提供商,它接受经过身份验证的用户的 ID,但是每当我 运行 我的应用程序时,它都会给我一个错误:
The getter 'uid' was called on null.
这是我的代码:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// ignore: deprecated_member_use
AuthService _auth = new AuthService();
TimerService _timer;
User user;
List<Timing> timers = [];
initUser() async {
await _auth.signInAnon();
user = await _auth.currentUser();
print("THE UID IS ${user.uid}");
setState(() {});
}
@override
void initState() {
super.initState();
initUser();
}
@override
Widget build(BuildContext context) {
//TODO: implement firebase data req
return StreamProvider<List<Timing>>.value(
value: TimerService(user.uid).timingStream,
child: Scaffold(
backgroundColor: Styles.backgroundColor,
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => CreateTimer()));
},
child: Icon(Icons.add),
),
appBar: AppBar(
elevation: 0.1,
backgroundColor: Styles.backgroundColor,
centerTitle: true,
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.list),
onPressed: () {
//TODO
},
)
],
),
body: Column(children: [
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: [
TimerList(),
]),
),
),
]),
),
);
}
}
它还会因此抛出另一个错误:
Another exception was thrown: NoSuchMethodError: The getter 'length' was called on null.
这是由于我使用了所提供的数据:
class TimerList extends StatefulWidget {
TimerListState createState() => TimerListState();
}
class TimerListState extends State<TimerList> {
@override
Widget build(BuildContext context) {
final timingStream = Provider.of<List<Timing>>(context);
return ListView.builder(
itemCount: timingStream.length,
itemBuilder: (context, index) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[TimerWidget(timingStream[index])],
);
});
}
}
请向我解释为什么以及如何为提供商提供用户 uid 的正确方法。
如果我发布了过多的代码,我深表歉意,为了安全起见,抱歉。
在 Dart 中,默认情况下,如果您未分配任何值,则该值为 null
所以在你的情况下 User 是 null 所以创建一个新对象。
User user=new User();
现在,如果您想使用用户实例,那么您可以
initUser() async {
await _auth.signInAnon();
user = await _auth.currentUser();
print("THE UID IS ${user.uid}");
setState(() {});
}
在过去的几个月里,我一直在研究 Flutter,该死的,我被困在了这个上。
我想设置一个提供商,它接受经过身份验证的用户的 ID,但是每当我 运行 我的应用程序时,它都会给我一个错误:
The getter 'uid' was called on null.
这是我的代码:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// ignore: deprecated_member_use
AuthService _auth = new AuthService();
TimerService _timer;
User user;
List<Timing> timers = [];
initUser() async {
await _auth.signInAnon();
user = await _auth.currentUser();
print("THE UID IS ${user.uid}");
setState(() {});
}
@override
void initState() {
super.initState();
initUser();
}
@override
Widget build(BuildContext context) {
//TODO: implement firebase data req
return StreamProvider<List<Timing>>.value(
value: TimerService(user.uid).timingStream,
child: Scaffold(
backgroundColor: Styles.backgroundColor,
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => CreateTimer()));
},
child: Icon(Icons.add),
),
appBar: AppBar(
elevation: 0.1,
backgroundColor: Styles.backgroundColor,
centerTitle: true,
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.list),
onPressed: () {
//TODO
},
)
],
),
body: Column(children: [
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: [
TimerList(),
]),
),
),
]),
),
);
}
}
它还会因此抛出另一个错误:
Another exception was thrown: NoSuchMethodError: The getter 'length' was called on null.
这是由于我使用了所提供的数据:
class TimerList extends StatefulWidget {
TimerListState createState() => TimerListState();
}
class TimerListState extends State<TimerList> {
@override
Widget build(BuildContext context) {
final timingStream = Provider.of<List<Timing>>(context);
return ListView.builder(
itemCount: timingStream.length,
itemBuilder: (context, index) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[TimerWidget(timingStream[index])],
);
});
}
}
请向我解释为什么以及如何为提供商提供用户 uid 的正确方法。 如果我发布了过多的代码,我深表歉意,为了安全起见,抱歉。
在 Dart 中,默认情况下,如果您未分配任何值,则该值为 null 所以在你的情况下 User 是 null 所以创建一个新对象。
User user=new User();
现在,如果您想使用用户实例,那么您可以
initUser() async {
await _auth.signInAnon();
user = await _auth.currentUser();
print("THE UID IS ${user.uid}");
setState(() {});
}