title: Dart- Exception after run my flutter search app
body: <p>My question about how can i handle this exception error that appear on my mobile when i execute it with red screen<br />
  i'm new with flutter and  I have wrote simple search bar widget. After executing it got exception:</p>
  <pre><code> 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.'
  </code></pre>
  <p>This is main.dart for searchbar:</p>
  <pre><code>import 'package:flutter/material.dart';
  
  void main() {
    runApp(MyFirstApp());
  }
  
  class MyFirstApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(&quot;Search...&quot;),
          actions: &lt;Widget&gt;[
            IconButton(
                icon: Icon(Icons.search),
                onPressed: () {
                  showSearch(context: context, delegate: DataSearch());
                })
          ],
        ),
        drawer: Drawer(),
      );
    }
  }
  
  class DataSearch extends SearchDelegate&lt;String&gt; {
    final data1 = [&quot;Amr&quot;, &quot;Amir&quot;, &quot;Moatasem&quot;, &quot;Gamal&quot;, &quot;Tasneem&quot;];
    final data2 = [&quot;Amr&quot;, &quot;Amir&quot;];
    @override
    List&lt;Widget&gt; buildActions(BuildContext context) {
      return &lt;Widget&gt;[
        IconButton(
            icon: Icon(Icons.clear),
            onPressed: () {
              query = &quot; &quot;;
            })
      ];
    }
  
    @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) =&gt; p.startsWith(query)).toList();
      return ListView.builder(
        itemBuilder: (context, index) =&gt; 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,
      );
    }
  }
  </code></pre>
  <p>This is Widget test.dart</p>
  <pre><code>
  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);
    });
  }
  
  </code></pre>
  <p>I have found MediaQuery.of but don't understand how can it be used with existing widget? It accept BuildContext as parameter.</p>
  <pre><code> static MediaQueryData of(BuildContext context, { bool nullOk = false }) {
      assert(context != null);
      assert(nullOk != null);
      final MediaQuery query = context.dependOnInheritedWidgetOfExactType&lt;MediaQuery&gt;();
      if (query != null)
        return query.data;
  </code></pre>