Home > Software engineering >  Could not find the correct Provider<TaskData> above this TasksScreenNew Widget
Could not find the correct Provider<TaskData> above this TasksScreenNew Widget

Time:02-11

I have a problem using Flutter Provider... every time when im clicking at the widget that brings me to the Tasksscreen i'm getting this error. Im really new to Provider... got that code from a tutorial. I'm just stuck with this error for 1 hour now and i haven't found anything suitable for my problem.

If you need more code, just say it

Error:

ProviderNotFoundException (Error: Could not find the correct Provider<TaskData> above this TasksScreenNew Widget


Code:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:learnon/widgets/tasks_list.dart';
import 'package:learnon/screens/addtasksscreen.dart';
import 'package:learnon/models/tasks_data.dart';
import 'package:provider/provider.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

bool theme = false;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

class TasksScreenNew extends StatelessWidget {
  static const routeName = "/tasksnewwidget";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueAccent,
      floatingActionButton: FloatingActionButton(
        heroTag: null,
        child: Icon(Icons.add),
        backgroundColor: Colors.lightBlue,
        onPressed: () {
          showModalBottomSheet(
              isScrollControlled: true,
              context: context,
              builder: (BuildContext context) => AddTaskScreen());
        },
      ),
      body: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Container(
          padding: EdgeInsets.only(top: 60, left: 30, right: 30, bottom: 30),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 10,
              ),
              Text(
                'Todo',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 50,
                    fontWeight: FontWeight.bold),
              ),
              Text(
                '${Provider.of<TaskData>(context).taskCount} Tasks',
                style: TextStyle(fontSize: 18, color: Colors.white),
              ),
            ],
          ),
        ),
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 20),
            child: TasksList(),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(20),
                topRight: Radius.circular(20),
              ),
            ),
          ),
        )
      ]),
    );
  }
}
// CircleAvatar(
// radius: 30,
// backgroundColor: Colors.white,
// child: Icon(
// Icons.list,
// color: Colors.blueAccent,
// size: 30,
// ),
// )

CodePudding user response:

To access providers from context, you need to wrap any widget higher up the tree in Provider or ChangeNotifierProvider.

You can try doing the following on the current page.

Widget build(BuildContext context) {
  return ChangeNotifierProvider<TaskData>(
    create: (_) => TaskData(),
    builder: (context, __) => Scaffold(

You can also see examples, e.g. here https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple

  • Related