Hello everyone I'm trying to create a list contains values in .txt file and then chunk it into sublists. But when I try to return the chunk list I got this error:
A value of type 'List' can't be returned from the method 'localPath' because it has a return type of 'Future<List>
this is my code :
Future<List<int>> localPath() async {
final textasset = "assets/112936-bluetooth.txt";
final text = await rootBundle.loadString(textasset);
final bytes =
text.split(',').map((s) => s.trim()).map((s) => int.parse(s)).toList();
print(bytes);
final chunks=[];
int chunkSize = 20;
for (int i = 0; i < bytes.length; i = chunkSize) {
chunks.add(bytes.sublist(
i, i chunkSize > bytes.length ? bytes.length : i chunkSize));
print(chunks);
}
return chunks;
}
this is a screenshot
Thanks in advance for your help
CodePudding user response:
What you are doing at below line
chunks.add(bytes.sublist(i, i chunkSize > bytes.length ? bytes.length : i chunkSize));
is, you are adding a List<int>(which is the sublist of bytes) inside a List named chunks.
Which makes chunks dataType List<List<int>>
and your return type for the function localPath() is Future<List<int>> which is incorrect. It should be Future<List<List<int>>>.
Solution 1.
Future<List<List<int>>> localPath() async {
const textasset = "assets/112936-bluetooth.txt";
final text = await rootBundle.loadString(textasset);
final bytes = text.split(',').map((s) => s.trim()).map((s) => int.parse(s)).toList();
print(bytes);
final List<List<int>> chunks = [];
int chunkSize = 20;
for (int i = 0; i < bytes.length; i = chunkSize) {
chunks.add(bytes.sublist(i, i chunkSize > bytes.length ? bytes.length : i chunkSize));
print(chunks);
}
return chunks;
}
Solution 2.
If required you can try
chunks.addAll(bytes.sublist(i, i chunkSize > bytes.length ? bytes.length : i chunkSize));
it will merge all the sublists into chunks and you wont need to change above things.
Future<List<int>> localPath() async {
const textasset = "assets/112936-bluetooth.txt";
final text = await rootBundle.loadString(textasset);
final bytes = text.split(',').map((s) => s.trim()).map((s) => int.parse(s)).toList();
print(bytes);
final List<int> chunks = [];
int chunkSize = 20;
for (int i = 0; i < bytes.length; i = chunkSize) {
chunks.addAll(bytes.sublist(i, i chunkSize > bytes.length ? bytes.length : i chunkSize));
print(chunks);
}
return chunks;
}
CodePudding user response:
You are mixing up an asynchronous function with a function that returns a Future. Right now, you are using both, but you might not want to do that.
It is sufficient to mark your function as async
and return a List<int>
.
So you could change your function to
List<int> localPath() async {
// Rest of your code goes here...
// As you marked your function with async, you can use await here
return chunks;
}
CodePudding user response:
You can include the dataType on chunks
like
final List<int> chunks = [];
Use chunks.addAll
instead of .add
to return List<int>
.
Future<List<int>> localPath() async {
final textasset = "assets/112936-bluetooth.txt";
final text = await rootBundle.loadString(textasset);
final bytes =
text.split(',').map((s) => s.trim()).map((s) => int.parse(s)).toList();
print(bytes);
final List<int> chunks = [];
int chunkSize = 20;
for (int i = 0; i < bytes.length; i = chunkSize) {
chunks.addAll(bytes.sublist(
i, i chunkSize > bytes.length ? bytes.length : i chunkSize));
print(chunks);
}
return chunks;
}