Flutter BuildContext extension not working [solved]


Here is my extension file

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

extension BuildContextExtensions on BuildContext {
    void snack(String text){
      ScaffoldMessenger.of(this).showSnackBar(SnackBar(content: Text(text)));

Now when i try to call it context.snack("asd"); it shows that "The method ‘snack’ isn’t defined for the type ‘BuildContext". And if I hover on the context it says package:flutter/src/widgets/framework.dart BuildContext get context

And it doesn’t suggest me to import the extensions file. But when i copy the extension class into the file where i try to use it, then it works.
I had to manually import the file to make it work.


Solution was to manually import the file with the extension class into the file where it is used. Android studio didnt suggest me to import it. If i wrote the class name, only then it suggested to import it.

Answered By – lxknvlk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published