i trying to translate a string and show it to my screen but when i try to check the text on print it say Instance of Future<Translation>
this is my code when i try to use then(print)
final finalLastSeen = translator
.translate(timeago.format(userLastSeen), from: 'en', to: 'id')
.then(print);
it show the string that i want. but when i make this
final finalLastSeen = translator.translate(timeago.format(userLastSeen),
from: 'en', to: 'id');
print(finalLastSeen);
it will show the Future error
this is my whole FutureBuilder Code
return FutureBuilder(
future: users.doc(product.userId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return const Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return const Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
_sellerImageurl = '${data['imageUrl']}';
_sellerName = '${data['name']}';
_sellerJoinedDate = '${data['createdAt']}';
_sellerCity = '${data['location']}';
_sellerStatus = '${data['status']}';
_sellerLastSeen = '${data['lastSeen']}';
}
final DateTime lastSeen = DateTime.parse(_sellerLastSeen);
final Duration _sellerTimePassed = time.difference(lastSeen);
final userLastSeen = DateTime.now().subtract(_sellerTimePassed);
final _userOffline = '';
print(timeago.format(userLastSeen));
final finalLastSeen = await translator.translate(timeago.format(userLastSeen),
from: 'en', to: 'id');
print(finalLastSeen);
return product_details_layout(
cartProvider: cartProvider,
product: product,
productId: productId,
wishListProvider: wishListProvider,
sellerImageurl: _sellerImageurl,
sellerName: _sellerName,
sellerCity: _sellerCity,
sellerStatus: _sellerStatus,
userOffline: _userOffline,
productList: productList,
sellerJoinedDate: _sellerJoinedDate,
);
},
);
``
CodePudding user response:
The translate is an async function.
The then
is waiting for the translate function to complete and return your Translation
.
In the second case, you have no then
so you are not waiting for it to complete and you just have a Future<Translation
.
You can do this instead:
final finalLastSeen = await translator.translate(timeago.format(userLastSeen),
from: 'en', to: 'id');
print(finalLastSeen);
The await
will make sure you are waiting for the function to complete before you call the print.
CodePudding user response:
You should take care of async/await
. Please take look at here. If you are getting printed like Instance of Future<anything>
that means you are not waiting for the asynchronous to fulfill. Your code should look like this.
/// for printing promise result
translator.translate(timeago.format(userLastSeen), from: 'en', to: 'id')
.then(print);
/// or by await
final finalLastSeen = await translator
.translate(timeago.format(userLastSeen), from: 'en', to: 'id');
print(finalLastSeen);
/// inside widget you can use FutureBuilder
widget(
child: FutureBuilder(
future: translator.translate(timeago.format(userLastSeen), from: 'en', to: 'id'),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) => snapshot.hasData ? Text(snapshot.data):Text("Waiting...")
)
)
CodePudding user response:
You can see some example from the Translator package example.
So, if you want to have the string as result, use async
methode and put await
keyword before translator.translate
:
void myMethod() async {
final finalLastSeen = await translator.translate(timeago.format(userLastSeen), from: 'en', to: 'id');
print(finalLastSeen);
}