I have the issue of _CastError (Null check operator used on a null value, its happened in openstreet map included in Flutter project when i choose service of car in rider app and greate reuest of ride.
check the code :
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:ridy/generated/l10n.dart';
import '../graphql/generated/graphql_api.dart';
import 'bloc/main_bloc.dart';
import '../main/select_service_view.dart';
import 'package:velocity_x/velocity_x.dart';
class ServiceSelectionCardView extends StatelessWidget {
const ServiceSelectionCardView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final mainBloc = context.read<MainBloc>();
return BlocBuilder<MainBloc, MainBlocState>(builder: (context, state) {
return Mutation(
options: MutationOptions(document: CREATE_ORDER_MUTATION_DOCUMENT),
builder: (RunMutation runMutation, QueryResult? result) {
return Column(
children: [
FloatingActionButton.extended(
heroTag: 'cancelFab',
onPressed: () => mainBloc.add(ResetState()),
label: Text(S.of(context).action_cancel),
icon: const Icon(Icons.close))
.pOnly(bottom: 8)
.objectCenterRight(),
SelectServiceView(
data: (state as OrderPreview).fareResult,
onServiceSelect: (String serviceId, int intervalMinutes) async {
final args = CreateOrderArguments(
input: CreateOrderInput(
serviceId: int.parse(serviceId),
intervalMinutes: 0,
points: state.points
.map((e) => PointInput(
lat: e.point.latitude,
lng: e.point.longitude))
.toList(),
addresses:
state.points.map((e) => e.address).toList()))
.toJson();
final result = await runMutation(args).networkResult;
//print(result!.data!.toString());
final _order =
CreateOrder$Mutation.fromJson(result!.data!).createOrder;
mainBloc.add(OrderUpdated(order: _order));
},
),
],
);
},
);
});
}
}
CodePudding user response:
Try using the bang operator !
, when you're 100% sure that the variable can't be null
. Seems like result.data
can in fact be null
, if you log the result you will see that this is the case. You can try making a null
check first before serializing the response :
if (result!=null){
if (result!.data!=null){
// result.data here is never `null`
}
}
CodePudding user response:
Change this line
CreateOrder$Mutation.fromJson(result!.data!).createOrder;
with this one
CreateOrder$Mutation.fromJson(result.data).createOrder;
CodePudding user response:
Try below hope its help to you.you have put extra !
null operator remove it. Refer Flutter Sound null safety
here and here
Replace
result!.data!
to this
result!.data
Full declaration:
final _order = CreateOrder$Mutation.fromJson(result!.data).createOrder;
mainBloc.add(OrderUpdated(order: _order));