Flutter ile bir uygulama geliştiriyorum. Ekranı ikiye böldüm. Üst ekranda butonlar var. O butonlara bastıkça ekranın altındaki sayfalar değişecek.
Bu iş için ChangeNotifierProvider kullandım. Ama aşağıdaki hatayı verdi. Admin paneli açılmadan ekranı kırmızıya boyayarak bu hatayı veriyor.
Bu hataya sebep olacak durumlara da baktım, hiçbirinde sorun yok. Hata tam olarak nerede anlamadım. Hata mesajı: Error: Could not find the correct Provider<StateAltMenuData> above this AdminHomePage Widget
This happens because you used a `BuildContext` that does not include the providerof your choice. There are a few common scenarios:
- You added a new provider in your `main.dart` and performed a hot-reload. To fix, perform a hot-restart.
- The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then other routes will not be able to access that provider.
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
Make sure that AdminHomePage is under your MultiProvider/Provider<StateAltMenuData>. This usually happens when you are creating a provider and trying to read it immediately. Ekrandaki görüntüyü oluşturan kodlar (ilgili kod satırları : 3.4.15.71.86.) = void main() {
runApp(
ChangeNotifierProvider<StateAltMenuData>(
create: (BuildContext context) => StateAltMenuData(),
child: MaterialApp(
home: AdminHomePage(),
),
),
);
}
class AdminHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
Function altMenu = Provider.of<StateAltMenuData>(context).altMenuDegistir;
final screenHeight = MediaQuery.of(context).size.height;
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
flex: 3,
child: Stack(
children: <Widget>[
OpaqueImage(
color: primaryColorOpacity.withOpacity(0.85),
imageUrl: "assets/images/kitap_arkaplan.jpg",
),
SafeArea(
child: Padding(
padding: const EdgeInsets.all(4),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Align(
alignment: Alignment.centerLeft,
child: IconButton(
icon: const Icon(Icons.star),
color: Colors.white,
onPressed: () {
Navigator.pushAndRemoveUntil(
context,
PageTransition(
type: PageTransitionType.fade,
child: UserHomePage()),
(Route<dynamic> route) => false);
},
),
),
Align(
alignment: Alignment.topCenter,
child: Text(
"Yönetici Sayfası",
textAlign: TextAlign.center,
style: headingTextStyle,
),
),
Align(
alignment: Alignment.centerRight,
child: IconButton(
icon: const Icon(
Icons.supervisor_account_rounded),
color: Colors.white,
onPressed: () {},
),
),
],
),
ControlButtons(),
],
),
),
),
],
),
),
Expanded(
flex: 5,
child: SingleChildScrollView(
child: Container(
height: screenHeight * (5 / 8),
padding: const EdgeInsets.only(top: 0),
color: Colors.white,
child: altMenu(),
),
),
),
],
),
);
}
}State bilgisinin tutulduğu kodlar = class StateAltMenuData with ChangeNotifier {
int altMenuIndex = 1;
altMenuDegistir(int yeniIndex) {
altMenuIndex = yeniIndex;
notifyListeners();
switch (yeniIndex) {
case 1:
return Kutuphane();
break;
case 2:
return KitapAra();
break;
case 3:
return TalebeEkle();
break;
case 4:
return TalebeAra();
break;
default:
return Container(
child: Center(
child: Text("Bir şeyler ters gitti."),
),
);
}
}
}Butonlara ait kodlar (ilgili kod satırları : 4.11.23.39.51.) = class ControlButtons extends StatelessWidget {
@override
Widget build(BuildContext context) {
Function yeniIndex = Provider.of<StateAltMenuData>(context).altMenuDegistir;
return Table(
children: [
TableRow(
children: [
GestureDetector(
onTap: () {
yeniIndex(1);
},
child: ControlButtonCard(
icon: Icon(
Icons.add_circle_outline_rounded,
color: Colors.blue,
),
text: "Kütüphane",
),
),
GestureDetector(
onTap: () {
yeniIndex(2);
},
child: ControlButtonCard(
icon: Icon(
Icons.search_rounded,
color: Colors.blue,
),
text: "Kitap Ara",
),
),
],
),
TableRow(
children: [
GestureDetector(
onTap: () {
yeniIndex(3);
},
child: ControlButtonCard(
icon: Icon(
Icons.person_add,
color: Colors.blue,
),
text: "Talebe Ekle",
),
),
GestureDetector(
onTap: () {
yeniIndex(4);
},
child: ControlButtonCard(
icon: Icon(
Icons.person_search,
color: Colors.blue,
),
text: "Talebe Ara",
),
),
],
),
],
);
}
}