The function call is not returning null but still giving "null" to be returned error - flutter

I'm migrating my old version flutter code to latest version with null safety feature.
In a function call I am getting the error "The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type". I have enclosed my code in try catch block and in catch block I added rethrow statement to prevent null exception.
This is my code.
Future<Map<String, dynamic>> fetchTimeline(http.Client client) async {
try {
print('INVOICE URL: ${globals.ursl.getURL(URLS.GETINVOICEURL)}');
Response response;
Dio dio = new Dio();
response = await dio.get(globals.ursl.getURL(URLS.GETINVOICEURL));
print('INVOICE GET RESPONSE: $response');
if (response.statusCode == 200) {
Map mapobject = (json.decode(response.toString()));
var succes = mapobject['success'];
if (succes == 1) {
if (mapobject['Invoice'][0]['address'] == null ||
mapobject['Invoice'][0]['address'] == '') {
address = '';
} else {
address = mapobject['Invoice'][0]['address'];
}
if (mapobject['Invoice'][0]['contact'] == null ||
mapobject['Invoice'][0]['contact'] == '')
phone = '';
else
phone = mapobject['Invoice'][0]['contact'];
if (mapobject['Invoice'][0]['restaurant_name'] == null ||
mapobject['Invoice'][0]['restaurant_name'] == '') {
name = ' ';
} else {
name = mapobject['Invoice'][0]['restaurant_name'];
}
logo = mapobject['Invoice'][0]['logo'];
globals.invoiceData = mapobject['Invoice'][0];
startTime();
return mapobject['Invoice'][0];
} else {
return {};
}
}
} catch (error) {
client.close();
print("CONNECTION CLOSED: $error");
rethrow;
}
}
I have added rethrow in catch block but still error is there.
Anyone there to help me out.
Thanks

It's a little hard to see with all the nested if statements, but you aren't returning a Map<String, dynamic> in every branch. This condition if (response.statusCode == 200) { ... } does not have a corresponding else branch, and so if the statusCode is some value other than 200 you are not returning anything (which means you are implicitly returning null in that case).

Related

how can sollve Problem related to null safety: A nullable expression can't be used as a condition

ChangeFavoritesModel? changeFavoritesModel;
void changeFavorites(int productId) {
favorites?[productId] = !favorites[productId];
DioHelper.postData(
url: FAVORITES,
data: {
'product_id': productId,
},
token: token,
).then((value) {
changeFavoritesModel = ChangeFavoritesModel.fromJson(value.data);
print(value.data);
emit(ShopSuccessChangeFavoritesState());
}).catchError((error) {
emit(ShopErrorChangeFavoritesState());
});
}
here,an error occurs:
favorites?[productId] = !favorites[productId];
and say:-
A nullable expression can't be used as a condition. (Documentation)
Try checking that the value isn't 'null' before using it as a condition.
because favorites is nullable, so check null first
if (favorites != null) {
favorites[productId] = !favorites[productId];
}
or if you could do an early return
if (favorites == null) {
return;
}

Future is stuck on return statement and never completes the function future called from

getInitialTripData calls getCurrentLocationData with await but it never prints "coming here 1" and on getCurrentLocationData the function goes on till printing the data and then stuck on return statement i dont know what the issue is
setInitialTripData() async {
print("coming here");
MapLocation startingPoint=await Get.find<LocationService>().getCurrentLocationData();
print("coming here 1");
if (startingPoint != null) {
tripData.startingPoint = startingPoint;
startingPointTextController.text = startingPoint.name;
update();
}
}
Future<MapLocation> getCurrentLocationData()async{
try{
if(!locationAllowed.value){
return null;
}
LocationData position=await _location.getLocation();
List<geo.Placemark> placemark =
await geo.placemarkFromCoordinates(position.latitude, position.longitude);
if(placemark.length==0 || placemark==null){
return null;
}
MapLocation mapLocation=MapLocation(name: "${placemark[0].name.isNotEmpty? placemark[0].name+", ":""}${placemark[0].subAdministrativeArea.isNotEmpty? placemark[0].subAdministrativeArea+", ":""}${placemark[0].isoCountryCode.isNotEmpty? placemark[0].isoCountryCode:""}",latitude: position.latitude,longitude: position.longitude);
print(mapLocation.getDataMap());
return mapLocation;
}
catch(e){
return null;
}
}
getCurrentLocation has a bunch of chances to return null, and you're not handling a potential null return value in setInitialTripData
your code only continues executing if startingPoint != null it seems.
Well Problem is in your getCurrentLocationData function because your function is expecting a return value of type MapLocation because you have declared in your function it's return type here
Future<MapLocation> getCurrentLocationData(){}
That's why when you return null from this function this throws an error and break your functions.
What you need to do is either remove return type or make it nullable whichever works fine like :
Future<MapLocation?> getCurrentLocationData(){}
Or
Future getCurrentLocationData(){}
Apart from that you need to make the receiving variable nullable so that it can handle null data
MapLocation? startingPoint=await Get.find<LocationService>().getCurrentLocationData();

Flutter : Conditions must have a static type of 'bool'

I'm trying to learn firebase with flutter and i ran into this problem
here is my code :
FirebaseFirestore.instance
.collection('attendees')
.doc(user.uid)
.snapshots()
.listen((snapshot) {
if (snapshot.data() != null) {
if (snapshot.data()!['attending']) {
_attending = Attending.yes;
} else {
_attending = Attending.no;
}
} else {
_attending = Attending.unknown;
}
notifyListeners();
});
what is the solution ?
the exact problem is within this line :
if (snapshot.data()!['attending']) {
how can I rewrite this so i wont ruin the current functionality ?
I appreciate your help inadvance
The reason you are getting error -
Conditions must have a static type of 'bool'
because on line snapshot.data()!['attending'] an = sign is missing.
To make your code work just do
if (snapshot.data() != snapshot.data()!['attending']) {
_attending = Attending.yes;
} else {
_attending = Attending.no;
}
Understanding The Error
I would also like to point out that Dart a stricter language (more like Java in terms of 'truthy' values).
In JavaScript you can use any ‘truthy’ value in a conditional statement. In Dart you cannot use ‘truthy’ values. For example:
var name = 'Joe';
if (name) {
// do something...
OR
var a = 1
if(a){
//this would work in JavaScript
}
You cannot do such things in Java or Dart. The reason is that Dart requires that a condition is a bool true not just a 'truthy' value. You could correct the code by changing it to:
if (name.isNotEmpty)
OR
if(a==1)
{
//these == signs are really important
}
Just store the snapshot.data() to the local map variable and do the operations by that.
_attendingSubscription = FirebaseFirestore.instance
.collection('attendees')
.doc(user.uid)
.snapshots()
.listen((snapshot) {
final Map<String, dynamic>? data = snapshot.data();
if (data != null) {
_attending = data['attending'] ? Attending.yes : Attending.no;
} else {
_attending = Attending.unknown;
}
notifyListeners();
});

The input does not contain any JSON tokens (Blazor, HttpClient)

i have an http Get method like below
public async Task<Ricetta> GetRicettaByNome(string nome)
{
Ricetta exist = default(Ricetta);
var ExistRicetta = await appDbContext.Ricetta.FirstOrDefaultAsync(n => n.Nome == nome);
if(ExistRicetta != null)
{
exist = ExistRicetta;
return exist;
}
exist = null;
return exist;
}
It gets called by a controller like this:
[HttpGet("exist/{nome}")]
public async Task<ActionResult<Ricetta>> GetRicettaByNome(string nome)
{
try
{
if (string.IsNullOrEmpty(nome))
{
return BadRequest();
}
var result = await ricetteRepository.GetRicettaByNome(nome);
if (result != null)
return result;
return default(Ricetta);
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "NON HAI INTERNET!");
}
}
But when i call my api to get the resposne by an httpclient like this:
public async Task<Ricetta> GetRicettaByNome(string nome)
{
return await httpClient.GetJsonAsync<Ricetta>($"api/Ricette/exist/{nome}");
}
i got this error:
the input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.'
This is the expected result when you return null from your API. And default(Ricetta) is the same as null.
You will have to handle this some other way. GetJsonAsync<T>() is convenient shorthand when you know you will always have data. It is not the best option for dealing with null.
You can see (in dev tools) that the status code is 204 (No Content) for null. You can detect that or catch the error from GetJsonAsync.
Your error exist in your repository part where GetJsonAsync<>. You need to use HttpResponseMessage and check the content before Deserialize for example:
private async ValueTask<T> GetJsonAsync(string ur)
{
using HttpResponseMessage response = awiat _client.GetAsync(url);
//some method to validate response
ValidateResponse(response);
//then validate your content
var content = await ValidateContent(response).ReadAsStringAsync();
return JsonSerializer.Desrialize<T>(content, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
}
//Here is the method that you need
private HttpContent ValidateContent(HttpResponseMessage response)
{
if(string.IsNullOrEmpty(response.Content?.ReadingAsString().Result))
{
return response.Content= new StringContent("null",Encoding.UTF8, MediaTypeNames.Application.Json);
}
else
{
return response.Content;
}
}

Unable to upload documents in iOS platform using flutter

I am trying to upload a document to our server using Dio and file_picker flutter framework. This is working properly in the android platform, but it does not work in iOS platform. Below I have mentioned the code with the exception detail. Please guide me in fixing this issue.
Future<ProfileSuccess> postUserRegister(Map params, String subURL) async {
if (params['image'] != null && params['image'] != '') {
String fileImageName = params['image'];
fileImageName = fileImageName
.split('/')
.last;
params['image'] = UploadFileInfo(File(params['image']), fileImageName);
}
else {
params['image'] = '';
}
if (params['security_certificate'] != null &&
params['security_certificate'] != '') {
String fileSecrityName = params['security_certificate'];
fileSecrityName = fileSecrityName
.split('/')
.last;
params['security_certificate'] =
UploadFileInfo(File(params['security_certificate']), fileSecrityName);
}
else {
params['security_certificate'] = '';
}
if (params['upload_resume'] != null && params['upload_resume'] != '') {
String fileResumeName = params['upload_resume'];
fileResumeName = fileResumeName
.split('/')
.last;
params['upload_resume'] =
UploadFileInfo(File(params['upload_resume']), fileResumeName);
}
else {
params['upload_resume'] = '';
}
try {
FormData formData = FormData.from(params);
Response response = await _dio
.post(AppBaseURL.baseURL + 'user/$subURL',
data: formData /*json.encode(params)*/,
options:
new Options(contentType: ContentType.parse(
"application/x-www-form-urlencoded")));
if (response.statusCode == 200 || response.statusCode == 201) {
final Map parsed = response.data;
return ProfileSuccess.fromJson(parsed);
} else if (response.statusCode == 422) {
return ProfileSuccess.withError("The email has already been taken");
} else
return ProfileSuccess.withError('Authentication Error');
}
on DioError catch (exception) {
if (exception == null || exception.type == DioErrorType.DEFAULT) {
return ProfileSuccess.withError(exception.message);
}
else if (exception.type == DioErrorType.RECEIVE_TIMEOUT ||
exception.type == DioErrorType.CONNECT_TIMEOUT) {
return ProfileSuccess.withError(
"Couldn't connect, please ensure you have a stable network.");
}
else if (exception.response.statusCode == 422) {
return ProfileSuccess.withError("The email has already been taken");
}
else {
return ProfileSuccess.withError("Something went wrong");
}
}}
FileSystemException: Cannot retrieve length of file' (OS Error: No such file or directory, errno = 2)