'How to get data on initState - Flutter Web Behavior

I'm getting a variable (number 4999 as example) from Firebase into the initState as follows:

class AsyncInitTestPage extends StatefulWidget {
  const AsyncInitTestPage({Key? key}) : super(key: key);


  @override
  _AsyncInitTestPageState createState() => _AsyncInitTestPageState();
}


class _AsyncInitTestPageState extends State<AsyncInitTestPage> {

  LoteActualBloc estanqueBloc = LoteActualBloc();

  String numtext = 'Previous';

@override
  void dispose() {
    estanqueBloc.dispose();
    super.dispose();
  }

@override
void initState() {
    _getNumberOnce();
  super.initState();  
}


Future<void> _getNumberOnce() async {
  int _num = await estanqueBloc.getNumber();
  numtext = _num.toString();
  print('Number on InitState $numtext');
  setState(() {});
}

  @override
  Widget build(BuildContext context) {

    print('Number on Build $numtext');

    return Scaffold(
      appBar: AppBar(
        title: Text('Async InitState Test'),
      ),
    );
  }
}

The Firebase RTDB query is pretty simple:

 Future<int> getNumber() async {

  Query resp = db.child('PATH/number');
    return resp.once().then((snapshot) => (snapshot.snapshot.value as dynamic));
} 

On mobile, I am getting the data as expected...

flutter: Number on Build Previous

flutter: Number on InitState 4999

flutter: Number on Build 4999

When I execute the same code on Web, I'm not getting the data...

Number on Build Previous

Are there differences in async functions in Web and Mobile?

How can I fix the code so that it works correctly in both environments (Web & Mobile)?

APPEND:

I am using firebase_database version: ^9.0.8 and Flutter 2.10.3. Before updating Flutter and using version 8.2.0 the code worked correctly in both environments.

If I downgrade to firebase_database^8.2.0, and make the corresponding changes (snapshot.value instead snapshot.snapshot.value) the queries work fine on Web again



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source