repository.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
class AnswersRepository {
getAnswers() async {
FirebaseFirestore.instance.collection("answers").get()
.then((querySnapshot) {
for (var result in querySnapshot.docs) {
return result.data();
print("SSSSSSSSSSSSSSS ${result.data()}");
}
});
}
}
the state
class AnswersSuccessState extends AnswersState {
Map<String, dynamic> answers;
AnswersSuccessState({required this.answers});
}
the bloc
class AnswersBloc extends Bloc<AnswersEvent, AnswersState> {
final AnswersRepository repo;
AnswersBloc(this.repo) : super(AnswersLoadingState()) {
on<AnswersEvent>((event, emit) async{
emit(AnswersLoadingState());
try {
final answers = await repo.getAnswers();
emit(AnswersSuccessState(answers: answers));
}catch(e) {
emit(AnswersErrorState(msg: e.toString()));
}
});
}
}
main.dart
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp( MyApp());
}
class MyApp extends StatelessWidget {
final fb = Firebase.initializeApp();
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Quick Answers',
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.deepPurpleAccent,
fontFamily: 'Georgia',
),
home: Scaffold(
body: FutureBuilder(
future: fb,
builder: (context, snapshot) {
if(snapshot.hasError) {
print(snapshot.error);
return const Center(child: Text("something wrong",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),),);
} else if (snapshot.hasData) {
return RepositoryProvider<AnswersRepository>(
create: (context) => AnswersRepository(),
child: MultiBlocProvider(
providers: [
BlocProvider<AnswersBloc>(
create: (context)
=> AnswersBloc(context.read<AnswersRepository>())
..add(FetchAnswers()))
],
child: HomeScreen()
),
);
} else {
return const Center(child: CircularProgressIndicator(),);
}
}),
)
);
}
}
UI
child: BlocConsumer<AnswersBloc, AnswersState>(
listener: (context, state) {},
builder: (context, state) {
if(state is AnswersLoadingState) {
return const Center(child: CircularProgressIndicator(),);
} else if(state is AnswersSuccessState) {
GridView.builder(
itemCount: 20,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2) ,
itemBuilder: (context, index) {
return CardWidget(
color: Colors.lightBlueAccent,
title: state.answers[index].title,
link: state.answers[index].link,);
}
);
} else if(state is AnswersErrorState) {
return Center(child: Text(state.msg),);
}
return Container();
} ,
)
show me this error:
Launching lib\main.dart on sdk gphone x86 in debug mode...
Running Gradle task 'assembleDebug'...
ā Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Debug service listening on ws://127.0.0.1:55327/MSL098hcqm4=/ws
Syncing files to device sdk gphone x86...
W/DynamiteModule(24240): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(24240): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(24240): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
I/s.quick_answer(24240): The ClassLoaderContext is a special shared library.
I/s.quick_answer(24240): The ClassLoaderContext is a special shared library.
I/TetheringManager(24240): registerTetheringEventCallback:osama.atraqchi.answers.quick_answers
V/NativeCrypto(24240): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 295 native methods...
W/s.quick_answer(24240): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (greylist, reflection, allowed)
I/ProviderInstaller(24240): Installed default security provider GmsCore_OpenSSL
W/Firestore(24240): (24.2.2) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
W/Firestore(24240):
W/Firestore(24240): This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
W/Firestore(24240): (24.2.2) [WatchStream]: (60a1739) Stream closed with status: Status{code=UNAVAILABLE, description=Channel shutdownNow invoked, cause=null}.
W/DynamiteModule(24240): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule(24240): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller(24240): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
note: I have made all configurations to connect with firebase and initialize the firebase app successfully without any problem...