getting future string and saving state in flutter - flutter

I am trying to get the string value of a future, and saving state in flutter. user chooses the endTime and it should display on the UI untill it ends. however, I am getting the following error:
type 'String' is not a subtype of type 'Future<String>' in type cast
the method:
final Future<SharedPreferences> _prefs =
SharedPreferences.getInstance();
Future<String> _textLine = '' as Future<String>;
Future<String> fastTrue() async {
final SharedPreferences prefs = await _prefs;
String formattedDate = DateFormat('yyyy-MM-dd,
hh:mma').format(endTime);
final textLine = (prefs.getString('formattedDate') ??
Languages.of(context)!.setYourFastTime) as Future<String>;
setState(() {
_textLine = prefs.setString('formattedDate',
Languages.of(context)!.endTimeIs
+'\n$formattedDate').then((bool success) {
return textLine;
});
});
return textLine;
}
in initState():
#override
void initState() {
super.initState();
_textLine = _prefs.then((SharedPreferences prefs) {
return prefs.getString('formattedDate') ??
Languages.of(context)!.setEndTime +'\n'+DateFormat('yyyy-MM-dd,
hh:mma').format(DateTime.now());
});
then in my widget build():
Padding(padding: const EdgeInsets.only(top: 170),
child: FutureBuilder<String>(
future: _textLine,
builder: (BuildContext context,
AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const CircularProgressIndicator();
default:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return Text(
Languages.of(context)!.endTimeIs +
"\n${snapshot.data}"
);
}
}
})),
help me, pls, tried using hive, but was not able to get to save the state of the widget. Thanks!

This code throws the error because you try to cast a String to a Future<String>>, although it is a String.
Future<String> _textLine = '' as Future<String>;
If you want to declare a Future with a value, you can use the value method.
Future<String> _textLine = Future.value('');

Related

Flutter dart async await not working as expected

I am trying to check the internet connection of the mobile device. I am using below code to check the connectivity.
import 'package:flutter/material.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
class RedirectPage extends StatelessWidget {
final int? status;
#override
Widget build(BuildContext context) {
bool? isDeviceConnected;
() async {
print("a");
print(123);
isDeviceConnected = await checkConnection();
print(888);
};
if (isDeviceConnected != null && isDeviceConnected == false) {
return AppNetworkConnectivityHome();
} else{
return HomePage();
}
}
}
print(isDeviceConnected); //giving null for the first time and true or false on the second time.
Future<bool?> checkConnection() async {
bool a = false;
a = await InternetConnectionChecker().hasConnection;
print(a);
return a;
}
how to force wait for the await function to complete
You'd have to await the method call. You've currently defined it as an anonymous function, so depending on where and how you execute it there will be some differences. But it will work if you instead do something like this:
Future<bool?> myMethod() async {
return await InternetConnectionChecker().hasConnection;
}
...
print(await myMethod());
You can't call async function in build method, you need to use FutureBuilder like this:
return FutureBuilder<bool>(
future: checkConnection(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading....');
default:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
bool data = snapshot.data ?? true;
if (!data) {
return AppNetworkConnectivityHome();
} else{
return HomePage();
}
}
}
},
)

flutter type 'List<dynamic>' is not a subtype of type 'FutureOr<List<Product>>'

I got FutureBuilder snapshot error when I parsing my JSON i got the:
type 'List' is not a subtype of type 'FutureOr<List>'
is it my Product model error or a parsing error?
my code
late Future<List<Product>> productFuture = getProducts();
static Future<List<Product>> getProducts() async {
var url = '${Constants.API_URL_DOMAIN}action=catalog&category_id=$id';
final response = await http.get(Uri.parse(url));
final body = jsonDecode(response.body);
print(body['data']);
return body['data'].map((e)=>Product.fromJson(e)).toList();
}
FutureBuilder<List<Product>>(
future: productFuture,
builder: (context, snapshot) {
print(snapshot);
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasData) {
final catalog = snapshot.data;
return buildCatalog(catalog!);
} else {
print('SNAPSOT DATA ${snapshot.data}');
return Text("No widget to build");
}
}),
Use List.from
return List.from(body['data'].map((e)=>Product.fromJson(e)));
static Future<List<Product>> getProducts() async {
var url = '${Constants.API_URL_DOMAIN}action=catalog&category_id=$id';
final response = await http.get(Uri.parse(url));
final body = jsonDecode(response.body);
print(body['data']);
return List.from(body['data'].map((e)=>Product.fromJson(e)));
}
Try to convert all List<Product> --> List<dynamic>

Integrate Provider with SharedPreferences to save and get Provider data

The list which stores the task data is integrated with provider for state management, but once I close the app and reopen it again, all tasks vanish.
With resources, I got to know about SharedPreferences.
How do I go about saving and getting the data using shared preferences. I have given the code a try, but does not seem to work in my favor.
void saveData() async {
final prefs = await SharedPreferences.getInstance();
final String encodedData = Task.encode(tasks);
await prefs.setString('task_data', encodedData);
}
void getData() async {
final prefs = await SharedPreferences.getInstance();
final String taskString = prefs.getString('task_data').toString();
List<Task> tasksData = Task.decode(taskString);
_tasks = tasksData;
}
encode() and decode() functions help in mapping List to String and String to List respectively.
static String encode(List<Task> tasks) {
return jsonEncode(
tasks.map<Map<String, dynamic>>((task) => Task.toMap(task)).toList(),
);
}
static List<Task> decode(String tasks) {
var data = (jsonDecode(tasks) as List<dynamic>?);
if (data != null) {
return (jsonDecode(tasks) as List<dynamic>?)!.map<Task>((task) {
return Task.fromJson(task);
}).toList();
} else {
return <Task>[];
}
}
The Task list in displayed using ListView.
Widget build(BuildContext context) {
return Consumer<TaskData>(
builder: (context, taskData, child) {
taskData.getData();
return ListView.builder(
itemCount: taskData.taskCount,
itemBuilder: (context, index) {
taskData.sortTaskList();
final task = taskData.tasks[index];
return TaskTile(
taskTitle: task.name,
isChecked: task.isDone,
checkboxCallBack: (checkBoxState) async {
taskData.upDateTask(task);
taskData.saveData();
},
longPressCallBack: () async {
taskData.removeTask(task);
taskData.saveData();
},
);
},
);
},
);
}
I am expecting that you're using ChangeNotifier with Provider package in TaskData class.
In this case you have to add notifyListener() inside getData() because it is async task and you are updating values.
Future<void> getData() async {
final prefs = await SharedPreferences.getInstance();
final String taskString = prefs.getString('task_data').toString();
List<Task> tasksData = Task.decode(taskString);
_tasks = tasksData;
notifyListener(); // Add this line
}

LateInitializationError: Field 'myfuture' has not been initialized

The issue that I am facing is in future builder in flutter.When opening the page first time the data is loaded successfully but when I go to a different page and then return to the same page it throws an error LateInitializationError: Field 'myfuture' has not been initialized.
Hence if you could please help me resolve this issue.
Please find below the code and let me know if any further information is required from my end.
view.dart
late final Future myfuture;
#override
void initState() {
print('init started'); // on opening second time the process gets stuck here with the above error message
if (Provider.of<FilterOptionProvider>(context, listen: false)
.initialList
.isEmpty) {
myfuture = Provider.of<FilterOptionProvider>(context, listen: false)
.readfilters(checkfilters);
}
super.initState();
}
Widget _buildList() {
final notificationData =
Provider.of<FilterOptionProvider>(context, listen: true);
final ndata = notificationData.initialList;
return FutureBuilder(
future: myfuture,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: LinearProgressIndicator());
} else if (snapshot.error != null) {
return const Center(
child: Text('An error occured'),
);
} else {
final notificationData =
Provider.of<FilterOptionProvider>(context, listen: true);
final ndata = notificationData.initialList;
provider.dart
Future<void> readfilters(Map<String, dynamic> queryPam) async {
Map<String, String> headers = {
"Content-Type": "charset=utf-8",
"Content-type": "application/json"
};
Just init the empty future in else,
if (Provider.of<Filter...) {
...
}
else {
myFuture = Future(() {});
}

Chained async methods don't show result in Widget with FutureBuilder

I am having a problem displaying asynchronous data in a text widget.
I'm doing two chained asynchronous methods to search for the coordinates and then search for the city from the smartphone:
Future<String> _getCity() async {
Future<Position> pos = Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
String city = "";
pos.then((result) {
return result;
})
.then((result) async {
List<Placemark> listPlacemark = await Geolocator().placemarkFromPosition(result);
return listPlacemark;
})
.then((result) {
return result.first;
})
.then((result) {
city = result.subAdministrativeArea;
// print( city ); -> Here it's showing correct data in console
});
return city;
}
The city appears on the console in that print command that is commented out.
To fill the text widget I'm doing this:
Padding(
padding: EdgeInsets.all(12),
child: FutureBuilder<String>(
future: _getCity(), // a Future<String> or null
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Error loading location');
case ConnectionState.waiting:
return Text('Waiting...');
default:
if (snapshot.hasError) {
return Text('Error loading location');
} else {
return Text(snapshot.data);
}
}
},
),
),
I called the _getCity method inside the initState method and it also worked.
The waiting message has already appeared but now everything is blank, what is missing?
I thank you for your attention!
Even if you return a value from the callback of then(), the value is not returned from _getCity(). You need to return Future.
Future<String> _getCity() async {
Future<Position> pos = Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
return pos.then((result) {
return result;
}).then((result) async {
List<Placemark> listPlacemark = await Geolocator().placemarkFromPosition(result);
return listPlacemark;
}).then((result) {
return result.first;
}).then((result) {
return result.subAdministrativeArea;
});
}
By the way, do you need so many thens?
I've never used Geolocator(), so this is just a guess, but some of them may be removed because it looks like listPlacemark, result.first and result.subAdministrativeArea are not Future and you just want to extract a value from List<Position>. If my guess is right, the following will do.
Future<String> _getCity() async {
Future<Position> pos = Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
return pos.then((result) async {
List<Placemark> listPlacemark = await Geolocator().placemarkFromPosition(result);
return listPlacemark.first.subAdministrativeArea;
});
}