Firebase always return null, however the data exsitis. Flutter Firebase
I am using a BLoC pattern to develop a Flutter application. I have 2 very similar screens (both use different blocs, but the other things are familiar: Screens use bloc and stream builder. BLoCs on both screens call repository methods with the same structure, but different return types). But the first screen works properly: BLoC calls the function from the repository and it returns the object, then the screen's StreamBuilder detects the object and uses the object properties, showing them on the screen. The second screen also uses BloCs. BLoC calls a repository function, which tries to retrieve data from Firebase (but this GET method always returns null and I get the message:
snapshot.error : NoSuchMethodError: The method '[]' was called on null.
) and create the object using this data, to use it the second screen's StreamBuild.
I would be very thankful if someone will help me as I have already spend a few hours on this issue.
Here are the BLoC provider, presented on the second screen and the StreamBuilder, also presented on this page:
Widget build(BuildContext context) {
return BlocBuilder<SlotConfigurationBloc, SlotConfigurationState>(
builder: (context, state) {
if (state is SlotConfigurationInitial) {
final slotConfigurationBloc =
BlocProvider.of<SlotConfigurationBloc>(context);
slotConfigurationBloc.add(
GetSlotConfiguration(
staff.staffID,
),
);
return buildTimeIntervalsLoading();
}
if (state is SlotConfigurationLoading) {
return buildTimeIntervalsLoading();
}
if (state is SlotConfigurationLoaded) {
return buildTimeIntervals(
timeIntervalWidth, outerIndex, state.slotConfiguration);
}
},
);
}
Widget buildTimeIntervals(double width, int outerIndex,
Stream<SlotConfigurationModel> slotConfiguration) {
return StreamBuilder(
stream: slotConfiguration,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Container();
}
switch (snapshot.connectionState) {
case ConnectionState.waiting:
print('Connection State : buildTimeIntervals');
break;
default:
final SlotConfigurationModel slotConfigurationModel = snapshot.data;
return Container(
width: width,
height: 300,
child: ListView.separated(
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
height: 10,
);
},
primary: false,
shrinkWrap: true,
itemCount: slotConfigurationModel.monNum ?? 20,
itemBuilder: (BuildContext context, int index) => TimeButton()),
);
}
},
);
Here is the BLoC:
class SlotConfigurationBloc
extends Bloc<SlotConfigurationEvent, SlotConfigurationState> {
final SlotConfigurationRepository repository;
SlotConfigurationBloc(this.repository);
@override
get initialState => SlotConfigurationInitial();
@override
Stream<SlotConfigurationState> mapEventToState(SlotConfigurationEvent event,) async* {
yield SlotConfigurationLoading();
if (event is GetSlotConfiguration) {
try {
yield SlotConfigurationLoading();
var slotConfiguration = repository.getSpecificSlotConfiguration(event.slotId);
yield SlotConfigurationLoaded(slotConfiguration);
}
}
}
}
Here is the repository, with a function, that is called by BLoC:
class SlotConfigurationRepository {
final slotConfigurationCollection = Firestore.instance
.collection('SERVICEPROVIDERINFO')
.document('tI7MXfOpg7fu81QOg4y0')
.collection('APPOINTMENTSLOTCONFIGURATION');
///
/// GET SPECIFIC SLOT CONFIGURATION
///
Stream<SlotConfigurationModel> getSpecificSlotConfiguration(String staffSlotId) {
try {
return slotConfigurationCollection
.document('$staffSlotId')
.snapshots()
.map((obj) {
return SlotConfigurationModel.fromFirestore(obj);
}
);
} on PlatformException catch (e) {
print(e);
return null;
} catch (err) {
print(err);
return null;
}
}
}
When I move to the second screen I get this error:
snapshot.error : NoSuchMethodError: The method '[]' was called on null.
Here is the screenshot
