Home > Blockchain >  The property can't be unconditionally accessed because the receiver can be 'null'
The property can't be unconditionally accessed because the receiver can be 'null'

Time:01-27

Hi I'm having two problems when trying to import API's into flutter, first it seems it seems it is unable to read the data from my snapshot so when I use the function if (snapshot.hasData) it keeps showing the CircularProgressIndicator.

Second problem is when I try to add a variables to allow me to add text from the API it is giving me the error 'The property can't be unconditionally accessed because the receiver can be 'null''`

@override
 Widget build(BuildContext context) {
    return Scaffold (
      appBar: AppBar(
        title: Text('News App'),
      ),
      body: Container(
        child: FutureBuilder<Welcome>(
          future: _Welcome,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: 52,
                itemBuilder:(context, index) {
              var ticker = snapshot.data.ticker; //error here
              return Container(
                height: 100,
                child: Row(
                  children: [
                    Text(ticker)
                  ],
                ),
              );
            });
            } 
            else 
            return Center(child: CircularProgressIndicator());
          },
          
        ),
      )
    );
  }`

CodePudding user response:

Make snapshot.data! not nullable like so:

var ticker = snapshot.data!.ticker; //error fixed here

CodePudding user response:

For your first problem try it with

if (snapshot.connectionState == ConnectionState.done)

For your second problem try adding an exclamation mark to guarantee the value is not null

var ticker = snapshot.data!.ticker;
  •  Tags:  
  • Related