print messages (data) from firebase cloud - flutter

I want to get data from firebase cloud
Data means my chatApp's messages.
but in my code i got error
android studio suggest me to add nullcheck(!) to
final messages = snapshot.data!.documents;
but it not working

Try to add the null-check and fix your typo: messgeText => messageText.

You r using Flutter 2.5.0 and now for the latest flutter version the syntax are changed for Firebase . Try this way:
final message = snapshot.data!.data()

Related

Flutter - Riverpod Future Provider: How to keep old data when error occurs

This is my usecase:
data is downloaded
final dataProvider = FutureProvider<MyModel>((ref) async {
return fetchData();
});
and it's used like this within widget's build method:
ref.watch(dataProvider).when(
data: DataWidget(data),
error: ErrorWidget(),
loading: LoadingWidget())
user has option to refresh the data:
ref.refresh(dataProvider.future);
But when there is an error (for example phone is in airplane mode) error is provided so DataWidget is lost and replaced with ErrorWidget... Is there a way using Riverpod to provide/keep existing data instead of error ? I believe it's common scenario, but I didn't find any elegant solution for this problem. I've also read documentation too, but didn't find anything helpful related to this. Am I missing something ? (I'm new to Riverpod) Thank you
Preserving the previous data on refresh is behavior as part of 2.0.0.
Although there were some bugs that you may have encountered. Make sure youre using 2.1.3 or above, which should fix all the issues related to this.
As for using when to show the previous data/error, you can use various flags:
asyncValue.when(
// show previous data/error on loading
skipLoadingOnReload: true,
// show previous data if there's an error
skipError: true,
loading:...,
data:...,
error: ...,
)
In the new version of Riverpod (as of v.2.1.0), you can do this:
asyncValue.when(
skipLoadingOnReload: false,
skipLoadingOnRefresh: true,
skipError: false,
data: DataWidget(data),
error: ErrorWidget(),
loading: LoadingWidget(),
)
You can see more details here.

Flutter String Interpolation remain dolloar sign after compiled

I'm using code like below
get('/api/products/detail/$name/')
it seems working fine for most of cases as I expected like below
// If variable value is 'apple'
get('/api/products/detail/apple/')
but sometimes error are catched by sentry and sentry says
My request's url was '/api/products/detail/$apple/'
This happens not all the time and it makes me so unstable
Is there anybody why this is happening and how to prevent this?
I'm using flutter 2.8.1 and dart version 2.15.1
Try below code: refer string-interpolation
var fruitName = "apple";
var apiURL = "/api/products/detail/$fruitName/";
print(apiURL);
your URL- /api/products/detail/apple/

Convert firebase stream to an mongodb stream in flutter

In my flutter project i have streams from firebase like this:
final CollectionReference _peopleRef=FirebaseFirestore.instance.collection('people');
List<People> _peopleListFromSnapshot(QuerySnapshot snapshot){
return snapshot.docs.map((doc) {
return People.fromJson(doc);
}).toList();
}
Stream<List<People>> getListAllPeopleStream(){
return _peopleRef.snapshots().map(_peopleListFromSnapshot);
}
Now i would like to receive the data from mongodb and after searching the documentation i guess i have to work with cursors and watch(), but the documentation doesn`t have code examples for flutter/dart.
Also i can`t find a tutorial via google, so i would highly appreciate any kind of help how to convert this stream to an mongodb change stream.
Let me know if you need more information!
Thanks!

Error: Expected a value of type 'Map<dynamic, dynamic>', but got one of type 'List<dynamic>'

I'm using hue_dart package and try to show me implemented lights in my hue bridge, but I get this Error:
Instance of `_Future<List<Light>>`
Error: Expected a value of type `Map<dynamic, dynamic>', but got one of type 'List<dynamic>'
How to convert this list to map, or how to display the list of lights?
This is my code:
class HueBridge {
searchBridge() async {
final client = Client();
final discovery = BridgeDiscovery(client);
List<DiscoveryResult> discoverResults = await discovery.automatic();
final discoveryResult = discoverResults.first;
final bridge = Bridge(client, discoveryResult.ipAddress);
print(discoveryResult.ipAddress);
final lights = bridge.lights();
print(lights);
}
}
For those who want to use the hue_dart package and encountered the same issue. A pull request have been written to fix this problem, it was accepted and merged but not released on the pub.dev.
Issue : https://github.com/Jairzinno/hue_dart/issues/8
PR : https://github.com/Jairzinno/hue_dart/pull/9
You have to know the hue_dart package is not developed by Philips, and seems to not be maintained. Indeed Philips released a V2 of their API but the package use the deprecated API V1.
To fix your issue you have 2 options :
Make the same changes as the PR (not recommended because in case of "flutter pub get" you will lose your updates).
Clone directly the GitHub of the package.
I'm actually using this package, but I'm thinking about creating a new hue_dart package to use the new V2 API.

Flutter - Parse Server SDK - IncludeObject not working with liveQuery

I was trying out includeObject method with my livequery in one of my project. I am using Parse_Server_Sdk for this back4App database. Also, I am using flutter parse_server_sdk latest version 1.0.26.
What i want is i have a list of events. So whenever a new event is added in my database then it should trigger the subscription method. I have used following code and it works as well. But, only thing i have issue with is i don't get the includedobject i have added in QueryBuilder.
QueryBuilder<ParseObject> parseQuery =
QueryBuilder<ParseObject>(ParseObject(tblEvents))
..whereEqualTo(tblEventUserId, currentUser);
parseQuery.includeObject([tblOrganizerId]);
Subscription subscription = await liveQuery.client.subscribe(parseQuery);
subscription.on(LiveQueryEvent.create, (value) {
print("val : ${value}");
print("val : ${value.runtimeType}");
});
Output :
val : {"className":"Group_Events","objectId":"uotQ6BpT4C","createdAt":"2020-08-27T07:18:26.581Z","updatedAt":"2020-08-27T07:18:26.581Z","organizer_id":{"__type":"Pointer","className":"Organizer","objectId":"t9M1oyZHh4"},"user_id":{"__type":"Pointer","className":"_User","objectId":"Hx5xJ5ABxG"},"weight":1}
I/flutter (30335): val : ParseObject
I have also evaluated the expression. But, there is only limited pointer data to "tblOrganizerId". I am not getting whole data of that table.
Can anyone suggest me a workaround or a solution?
Thanks.
I would use the "ParseLiveListWidget". It should be perfect for this scenario.
You can find an example here.
Additional information is in this README.
(Use version 1.0.27 for some important bug fixes.)