1

I am making an app using the BLoC architecture using the flutter_bloc package, but I need to get data from a database, which is asynchronous, meaning I can't initialize the BLoC with data from my database. Is there a way I can do this? My BLoC class text is

import 'package:countdown/database/utils.dart';
import 'package:countdown/events/countdown_event.dart';
import 'package:countdown/models/countdown.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class HomeBloc extends Bloc<CountdownEvent, List<Countdown>> {
   @override
  // TODO: implement initialState
  List<Countdown> get initialState => DatabaseUtils.getCountdowns();


  @override
  Stream<List<Countdown>> mapEventToState(CountdownEvent event) {
  }
}

I know this is very similar to This Question, but that question's answers don't have any code snippets which would be very helpful.

6
  • create one separate async function and call if from initState(). Commented May 18, 2020 at 22:28
  • initState() would still have to return a Future, which makes it an invalid override of the parent class. Commented May 18, 2020 at 23:39
  • " but I need to get data from a database, which is asynchronous," - so you need to read this amd this Commented May 19, 2020 at 3:57
  • The initialState has to be an instance of the type parameter State, which in my case is a List Commented May 19, 2020 at 13:02
  • and whats wrong with [] in that case? Commented May 19, 2020 at 13:05

1 Answer 1

1

create one separate async function and call if from initState(). this is my code you can call your method according to use.

  @override
  HomeState get initialState {
    _checkLocationSettings();
    return InitialHomeState();
  }



_checkLocationSettings() async {
locationUpdateSetting = await repository.isLocationUpdateOn();}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.