How can i call my provider model into initState method

168

is better don't do use Provider in initState, but you can use Future.delayed because you need context



@override
  void initState() {
    super.initState();
     // i tried this but it always give me errors that is isF  null value

   Future.delayed(Duration(seconds: 1), () {
  
     WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      isF = context.read<ProviderForFiltter>();
    }); 
       // also itried this but it don't work 
    isF = Provider.of<ProviderForFiltter>(context, listen: false);
   });
  }

Share:
168
Mohammed Hamdan
Author by

Mohammed Hamdan

Updated on January 03, 2023

Comments

  • Mohammed Hamdan
    Mohammed Hamdan over 1 year

    i have several widgets use my provider as a condition , and i need one call to access my provider to whole widget from init state instead of wrapping every widget into my provider and it's consumer

    this is my provider

    class ProviderForFiltter extends ChangeNotifier {
      bool isFiltterrr = true ;
    
      bool get isFiltter => isFiltterrr;
    
      void changeStatus(bool status){
        isFiltterrr = status;
        notifyListeners();
      }
    }
    

    this is my main.dart

    class Myproject extends StatefulWidget {
      const Myproject ({Key? key}) : super(key: key);
    
      @override
      _Myproject State createState() => _Myproject State();
    }
    
    class _Myproject State extends State<Myproject > {
    
    
    
      @override
      Widget build(BuildContext context) {
        return  
          
              Provider(
                   create: (BuildContext context) {
                    return ProviderForFiltter();
                   },
                   child: const MaterialApp(
                      debugShowCheckedModeBanner: false,
    
                      home: WelcomeScreen()
    
    
                ),
                 ),
    
        );
      }
    }
    

    this is my Stful Widget

    ProviderForFiltter? isF ;
     @override
      void initState() {
        super.initState();
         // i tried this but it always give me errors that is isF  null value
         WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
          isF = context.read<ProviderForFiltter>();
        }); 
           // also itried this but it don't work 
        isF = Provider.of<ProviderForFiltter>(context, listen: false);
      }
    
      @override
      Widget build(BuildContext context) {
    
    
    
    
        return Scaffold(
          body: Text('change'),
        )
    
      }
    
    }
    

    in the fact i need to use it's bool value as condition into Consumer and change it

    i hope any help guys

    • Tomas Ivan
      Tomas Ivan about 2 years
      Have you tried section I have an exception when obtaining Providers inside initState. What can I do? in pub.dev/packages/provider page?
  • Mohammed Hamdan
    Mohammed Hamdan about 2 years
    thanks but do you think this code is deserve to be Answer!? 1000 seconds like really ? and which code exactly to put it as what you mentioned since i have no idea what is the code exactly
  • Javad Zobeidi
    Javad Zobeidi about 2 years
    your provider code
  • Mohammed Hamdan
    Mohammed Hamdan about 2 years
    Can you please make favor to me and delete the answer , because no one would wait 1000 seconds , beside it didn't work too , and thank you so mush for trying to help Javad