1
votes

The problem is when I used mediaquery.of(context).size the error appears only in this specific external method.

I used the mediaquery in another class and it work successful and i tried to add context parameter in method but the error is not gone.

Error found:

I/flutter (24468): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (24468): The following assertion was thrown building Home(dirty, dependencies: [MediaQuery], state:

I/flutter (24468): HomeState#c4bef):

I/flutter (24468): 'package:flutter/src/widgets/media_query.dart': Failed assertion: line 715 pos 12: 'context !=

I/flutter (24468): null': is not true.

I/flutter (24468): I/flutter (24468): Either the assertion indicates an error in the framework itself, or we should provide substantially

I/flutter (24468): more information in this error message to help you determine and fix the underlying cause.

I/flutter (24468): In either case, please report this assertion by filing a bug on GitHub:

I/flutter (24468):
https://github.com/flutter/flutter/issues/new?template=BUG.md

I/flutter (24468):

I/flutter (24468): User-created ancestor of the error-causing widget was:

I/flutter (24468): Container

the code:

Widget slide() {
    return Container(
        height:  MediaQuery.of(context).size.width / 4,
        width: MediaQuery.of(context).size.width *1,
        child: Carousel(
          dotSize: 5,
          showIndicator: false,
          autoplay: true,
          images: [
            ExactAssetImage("images/01.jpg"),
            ExactAssetImage("images/02.jpg"),
            ExactAssetImage('images/03.jpg'),
            ExactAssetImage("images/04.jpg"),
            ExactAssetImage("images/05.jpg"),
            ExactAssetImage('images/06.jpg')
          ],
        ));
  }
1
I guess context is passed as a parameter in that method. Check if passed context is not null? Maybe you somehow pass a null there? - Filip P.
you are passing a null Context to MediaQuery.of(context) - pskink
I am passed nothing in the method - Batool AlFardan
Know I printed context the value was null so I pass BuildContext in the method then the problem is solved I don't know if the solution is correct or not - Batool AlFardan

1 Answers

1
votes

You need to pass the 'context' object to the method from the calling build() method to solve the error.