Wednesday, 3 March 2021

Flutter : Provider gets a null value

I am trying to create set a post to firestore using a provider, however, using breakpoints, I see that the providers value remains null, even after giving it a value. I have this code working in another code, but in here I fail to figure out why the providers value remains null.

Code in question from lib/widgets/create_page.dart

void addHabit(BuildContext ctx) {

      final habit = Habit(
        title: title.text,
      );

      final provider = Provider.of<TodosProvider>(ctx, listen: false);

      provider.addHabit(habit);

      Navigator.of(ctx).pop();
    }

And the provider from lib/providers/habits.dart

class TodosProvider extends ChangeNotifier {

  final FirebaseAuth auth = FirebaseAuth.instance;

   inputData() async{
    final User user = auth.currentUser;
    String uid = '123'; // FOR TESTING PURPOSE
    print('UID TEST');
    print(uid);
    return uid;
  }
  List<Habit> _habits = [];


  void setTodos(List<Habit> habits) =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        _habits = habits;
        notifyListeners();
      });

  void addHabit(Habit habit) async => FirebaseApi(uid:await inputData()).createHabit(habit);
}

enter image description here

Posting the github link as well, just in case https://github.com/sevenfold4/testing1



from Flutter : Provider gets a null value

No comments:

Post a Comment