Issue
keep getting "Null check operator used on a null value" error actually im trying to use providers to get values from database and use em in my app but im stuck at this i checked my code in every single way but still i keep getting this error
:lib\providers\user_provider.dart
import 'package:flutter/widgets.dart';
import 'package:test_app/models/user.dart';
import 'package:test_app/resources/auth_methods.dart';
class UserProvider with ChangeNotifier {
User? _user;
final AuthMethods _authMethods = AuthMethods();
User get getUser => _user!;
Future<void> refreshUser() async {
User user = await _authMethods.getUserDetails();
_user = user;
notifyListeners();
}
}
lib\pages\home.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_app/providers/user_provider.dart';
import 'package:test_app/models/user.dart' as model;
// import 'dart:html';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
void initState() {
super.initState();
addData();
}
addData() async {
UserProvider _userProvider =
Provider.of<UserProvider>(context, listen: false);
await _userProvider.refreshUser();
}
@override
Widget build(BuildContext context) {
model.User user = Provider.of<UserProvider>(context).getUser;
return Scaffold(
body: Center(
child: Text("this is homepage"),
),
);
}
}
debug snippet
#15 WidgetsBinding.drawFrame
package:flutter/…/widgets/binding.dart:882
#16 RendererBinding._handlePersistentFrameCallback
package:flutter/…/rendering/binding.dart:363
#17 SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1145
#18 SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1082
#19 SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:996
#23 _invoke (dart:ui/hooks.dart:150:10)
#24 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#25 _drawFrame (dart:ui/hooks.dart:114:31)
(elided 3 frames from dart:async)
idk what the problem actually im new to flutter and dart i've using python for my whole life
Solution
refreshUser
is a future and it sets _user
but because you're calling getUser
IMMEDIATELY build method, which basically returns _user
as a non-null value, the error would be thrown.
Try this:
class UserProvider with ChangeNotifier {
User? _user;
final AuthMethods _authMethods = AuthMethods();
User? get getUser => _user;
Future<void> refreshUser() async {
User user = await _authMethods.getUserDetails();
_user = user;
notifyListeners();
}
}
And your _HomeState
:
class _HomeState extends State<Home> {
@override
void initState() {
super.initState();
addData();
}
addData() async {
UserProvider _userProvider =
Provider.of<UserProvider>(context, listen: false);
await _userProvider.refreshUser();
}
@override
Widget build(BuildContext context) {
model.User? user = Provider.of<UserProvider>(context).getUser;
return Scaffold(
body: Center(
child: Text("this is homepage"),
),
);
}
}
Answered By - Josteve
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.