I need help, i tried to translate a list of strings in flutter with translator package, but it keep displaying _instance of 'Future<translation>' this is my code:
ListView.builder(
itemCount: docs.length,
itemBuilder: (context, index) {
translated((docs[index]['tweet']));
return ListTile(
title: Text(
docs[index]['name'],
style:
TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
),
subtitle: Text(
translator.translate(docs[index]['tweet']).toString()),
);
},
);
i tried to use async and await with setState() in a sperated method like this:
String output = "";
Future<void> translated(String post) async {
final Translation translation = await translator.translate(post, to: 'en');
final String out = translation.toString();
print(out);
setState(() {
output = out;
});
}
ListView.builder(
itemCount: docs.length,
itemBuilder: (context, index) {
translated((docs[index]['tweet']));
return ListTile(
title: Text(
docs[index]['name'],
style:
TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
),
subtitle: Text(output),
);
},
);
It does translate but the value keep on changing infinitly while displaying . this is the full body:
body: Container(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('favorite')
.doc(userID)
.collection('Publication')
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
List<dynamic> docs = snapshot.data.docs;
return FutureBuilder<List<String>>(
future: translated(),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading....');
default:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
List<String> data = snapshot.data ?? [];
return ListView.builder(
itemCount: docs.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
docs[index]['name'],
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold),
),
subtitle: Text(data[index]),
);
},
);
}
}
},
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
)),
- List item