This is my main.dart file, and I'm trying to do Application for notifications based on Location. Everything was going well, then I refreshed the app and I saw an error. I tried to pack widget, return, etc. in MaterialApp, but it didn't worked.
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:goe_notifications_new/search_places_screen.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:native_notify/native_notify.dart';
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState(){
return _HomePageState();
}
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
NativeNotify.initialize(624, 'sVEBBLTwolG1bSazpSwXa2', null);
runApp(HomePage());
}
class _HomePageState extends State<HomePage> {
LatLng _initialPosition = LatLng(37, -122);
late GoogleMapController controller;
Location _location = Location();
Marker _destination = Marker(markerId: const MarkerId('destination'));
// @override
// void dispose() {
// controller.dispose();
// super.dispose();
// }
Future createAlertDialog(BuildContext context){
TextEditingController customController = TextEditingController();
return showDialog(context: context, builder: (context){
return AlertDialog(
title: const Text("Your Todo"),
content: TextField(
controller: customController,
),
actions: [
MaterialButton(
elevation: 5.0,
child: const Text('Add TODO'),
onPressed : (){
// debugPrint("Hello");
Navigator.of(context).pop(customController.text.toString());
}
)
],
);
});
}
void _onMapCreated(GoogleMapController _cntrl) {
controller = _cntrl;
_location.onLocationChanged.listen((l) {
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(l.latitude!, l.longitude!),
zoom: 15
),
),
);
});
}
void _gotodestination(GoogleMapController _cntrl) {
controller = _cntrl;
_cntrl.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target: _destination.position,zoom: 14.5, tilt: 50 ),),);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: false,
title: const Text("GeoNavigation"),
actions: [
IconButton(
onPressed: () {},
icon: Icon(
Icons.search_outlined
),
)
],
),
body: GoogleMap(initialCameraPosition: CameraPosition(
target: _initialPosition, zoom: 10),
// onMapCreated: _onMapCreated,
myLocationEnabled: true,
myLocationButtonEnabled: true,
zoomControlsEnabled: false,
markers: {
if (_destination != null) _destination
},
onLongPress: _addMarker,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
onPressed: () {createAlertDialog(context).then((onValue){
SnackBar mySnackBar = SnackBar(content: Text("Added $onValue to TODOS"));
ScaffoldMessenger.of(context).showSnackBar(mySnackBar);
});
},
child: const Icon(Icons.note_add)
),
);
}
void submit(){
Navigator.of(context).pop();
}
void _addMarker(LatLng pos){
setState((){
_destination = Marker(
markerId: const MarkerId('destination'),
infoWindow: const InfoWindow(title: 'destination with 50m radius'),
position: pos,
);
});
}
}
And this is the Error, I've got when I Turn off the Emulator and refreshed the app:
======== Exception caught by widgets library =======================================================
The following assertion was thrown building HomePage(state: _HomePageState#fed07):
No MediaQuery widget ancestor found.
Scaffold widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was: Scaffold
dirty
state: ScaffoldState#a94f6(lifecycle state: initialized, tickers: tracking 2 tickers)
The ownership chain for the affected widget is: "Scaffold ← HomePage ← [root]"
No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
The relevant error-causing widget was:
HomePage HomePage:file:///C:/Users/simon_134zsx/AndroidStudioProjects/goe_notifications_new/lib/main.dart:18:10
When the exception was thrown, this was the stack:
#0 debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:230:7)
#1 debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:245:4)
#2 MediaQuery.of (package:flutter/src/widgets/media_query.dart:859:12)
#3 ScaffoldState.didChangeDependencies (package:flutter/src/material/scaffold.dart:2649:50)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4914:11)
#5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5)
#6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#7 Element.updateChild (package:flutter/src/widgets/framework.dart:3524:20)
#8 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#9 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#10 Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#11 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
#12 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#13 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#14 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#15 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
#16 SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:862:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
Please Help me Thanks :>