Mohammad Khashei
Mohammad Khashei

Reputation: 47

Why is this giving me an error? And how can I fix it (context error)

I used this code line elsewhere but it does not give an error

AutoSizeText(
        "عنوان"
        ,maxLines: 1
        ,overflow: TextOverflow.ellipsis,
        minFontSize: 10,
        style:GoogleFonts.rubik(fontSize: 25,color: Theme.of(context).indicatorColor,fontStyle: FontStyle.normal,fontWeight: FontWeight.w500),
      ),

Picture 1 enter image description here

Upvotes: 0

Views: 511

Answers (1)

Yatinj Sutariya
Yatinj Sutariya

Reputation: 520

You extracted widget generateItem() outside the state class, _picturesState.

So you have to pass the context of that state class as an argument of the generateItem(). Then you are allowed to use that context inside of Theme.of(context).

Your final solution is:

Card generateItem(BuildContext context) {
  return Card(
    //..
  );
}

And when you call this widget, simply pass context as an argument like:

return Container(
  child: Column(
    children:[
      generateItem(context);
    ]
  )
);

This should be inside of your state class which has its own context.

Upvotes: 1

Related Questions