Flutter Provider Error: Could not find the correct Provider above this Builder Widget. In Floating Action Button

I’m encountering an issue in my Flutter app where I’m getting the error message: “Error: Could not find the correct Provider<GroupService> above this Builder Widget.” I’ve wrapped my Home widget with ChangeNotifierProvider<GroupService>, but I’m still facing this issue.

Home.dart

import 'package:flutter/material.dart';
import 'package:attendance_app/services/group_service.dart';
import 'package:attendance_app/components/attendance_group_cards.dart';
import 'package:attendance_app/components/add_group_dialog.dart';
import 'package:provider/provider.dart';

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<GroupService>(
      create: (context) => GroupService(),
      child: Scaffold(
        appBar: AppBar(
          title: const Text(
            'ATTENDANCE APP',
            style: TextStyle(fontWeight: FontWeight.w700, color: Colors.white),
          ),
          centerTitle: true,
          backgroundColor: Colors.blue[500],
        ),
        body: Consumer<GroupService>(
          builder: (context, groupService, child) {
            return AttendanceGroupCards(
              groupService: groupService,
              urduStyle: const TextStyle(
                fontFamily: 'Jameel_Noori',
                fontSize: 24,
              ),
              englishStyle: const TextStyle(
                fontFamily: 'Lato',
                fontSize: 18,
                fontWeight: FontWeight.normal,
              ),
            );
          },
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () => _showAddGroupDialog(context),
          icon: const Icon(Icons.add),
          label: const Text("Add Group"),
          backgroundColor: Colors.blue[500],
          foregroundColor: Colors.white,
          splashColor: Colors.white10,
        ),
      ),
    );
  }

  void _showAddGroupDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AddGroupDialog(
          onAddGroup: (String groupName) {
            Provider.of<GroupService>(context, listen: false)
                .addGroup(groupName);
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text("Added $groupName"),
                duration: const Duration(seconds: 2),
              ),
            );
          },
        );
      },
    );
  }
}

group_service.dart

import 'package:flutter/foundation.dart'; 
import 'package:attendance_app/models/attendance_data.dart';

class GroupService extends ChangeNotifier {
  final List<Group> _groups = [
   
  ];
  List<Group> get groups => List.unmodifiable(_groups);

  //

  void addGroup(String groupName) {
    Group newGroup = Group(
      groupId: DateTime.now().toIso8601String(),
      groupName: groupName,
    );
    _groups.add(newGroup);
    notifyListeners();
  }

  void deleteGroup(String groupId) {
    _groups.removeWhere((group) => group.groupId == groupId);
    notifyListeners();
  }

  void editGroup(String groupId, String newGroupName) {
    _groups.forEach((group) {
      if (group.groupId == groupId) {
        group.groupName = newGroupName;
      }
    });
    notifyListeners();
  }
}



I wrapped my Home widget with ChangeNotifierProvider to provide the GroupService throughout the widget tree. However, when I attempted to access the GroupService using Provider.of(context) within a method called _showAddGroupDialog(BuildContext context), I encountered the error message: “Error: Could not find the correct Provider above this Builder Widget.”

To resolve this issue, I ensured that the Home widget is within the subtree where the provider is defined, and I confirmed that the context used to access the provider is within this subtree. Additionally, I verified that there are no conditional statements or error boundaries interfering with the provider initialization.

Despite these efforts, the error persisted. I’m seeking insights or suggestions on why this error might be occurring and how I can resolve it.

According to a similar Question it says..

When you use showDialog() method, the inner BuildContext and outer BuildContext can get tangled up which implies potential misbehaviors for your UI. To clear things out, you should declare your common class at the beginning of build(), then reuse it within the AlertDialog (which is also a best practice for reusing code):

So I tried this and it also does not seem to work

import 'package:flutter/material.dart';
import 'package:attendance_app/services/group_service.dart';
import 'package:attendance_app/components/attendance_group_cards.dart';
import 'package:attendance_app/components/add_group_dialog.dart';
import 'package:provider/provider.dart';

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<GroupService>(
      create: (context) => GroupService(),
      child: Scaffold(
        appBar: AppBar(
          title: const Text(
            'ATTENDANCE APP',
            style: TextStyle(fontWeight: FontWeight.w700, color: Colors.white),
          ),
          centerTitle: true,
          backgroundColor: Colors.blue[500],
        ),
        body: Consumer<GroupService>(
          builder: (context, groupService, child) {
            return AttendanceGroupCards(
              groupService: groupService,
              urduStyle: const TextStyle(
                fontFamily: 'Jameel_Noori',
                fontSize: 24,
              ),
              englishStyle: const TextStyle(
                fontFamily: 'Lato',
                fontSize: 18,
                fontWeight: FontWeight.normal,
              ),
            );
          },
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () => _showAddGroupDialog(
              context,
              Provider.of<GroupService>(context,
                  listen: false)), // Pass the context of Home widget
          icon: const Icon(Icons.add),
          label: const Text("Add Group"),
          backgroundColor: Colors.blue[500],
          foregroundColor: Colors.white,
          splashColor: Colors.white10,
        ),
      ),
    );
  }

  _showAddGroupDialog(BuildContext context, groupListModel) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AddGroupDialog(
          onAddGroup: (String groupName) {
            groupListModel.addGroup(groupName);
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text("Added $groupName"),
                duration: const Duration(seconds: 2),
              ),
            );
          },
        );
      },
    );
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật