My question about how can i handle this exception error that appear on my mobile when i execute it with red screen
i'm new with flutter and I have wrote simple search bar widget. After executing it got exception:
ErrorSummary('MediaQuery.of() called with a context that does not contain a MediaQuery.'),
ErrorDescription(
'No MediaQuery ancestor could be found starting from the context that was passed '
'to MediaQuery.of(). This can happen because you do not have a WidgetsApp or '
'MaterialApp widget (those widgets introduce a MediaQuery), or it can happen '
'if the context you use comes from a widget above those widgets.'
This is main.dart for searchbar:
import 'package:flutter/material.dart';
void main() {
runApp(MyFirstApp());
}
class MyFirstApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Search..."),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(context: context, delegate: DataSearch());
})
],
),
drawer: Drawer(),
);
}
}
class DataSearch extends SearchDelegate<String> {
final data1 = ["Amr", "Amir", "Moatasem", "Gamal", "Tasneem"];
final data2 = ["Amr", "Amir"];
@override
List<Widget> buildActions(BuildContext context) {
return <Widget>[
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = " ";
})
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed: () {
close(context, null);
});
}
@override
Widget buildResults(BuildContext context) {
return Container(
child: Center(
child: Text(query),
),
);
}
@override
Widget buildSuggestions(BuildContext context) {
final suggestion = query.isEmpty
? data2
: data1.where((p) => p.startsWith(query)).toList();
return ListView.builder(
itemBuilder: (context, index) => ListTile(
onTap: () {
showResults(context);
},
leading: Icon(Icons.question_answer),
title: RichText(
text: TextSpan(
text: suggestion[index].substring(0, query.length),
style:
TextStyle(color: Colors.black, fontStyle: FontStyle.italic),
children: [
TextSpan(
text: suggestion[index].substring(query.length),
style: TextStyle(color: Colors.red))
]),
),
),
itemCount: suggestion.length,
);
}
}
This is Widget test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:testtest/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyFirstApp());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
I have found MediaQuery.of but don't understand how can it be used with existing widget? It accept BuildContext as parameter.
static MediaQueryData of(BuildContext context, { bool nullOk = false }) {
assert(context != null);
assert(nullOk != null);
final MediaQuery query = context.dependOnInheritedWidgetOfExactType<MediaQuery>();
if (query != null)
return query.data;
question from:
https://stackoverflow.com/questions/65643406/dart-exception-after-run-my-flutter-search-app