Updating cloud firestore data in a map from flutter - flutter

This is my code:
child: new GridView.count(
crossAxisCount: 3,
children: snapshot.data.documents.map<Widget>((DocumentSnapshot document) {
return new MoodButton(
iconData: (IconData(document.data()['ref'],fontFamily: 'MaterialIcons')),
onTap: () =>
document.data()[document.data()['ref']].updateData({"display": true}),
);
}).toList(),
)
I am trying to update the value of display for the icon that has been clicked on from false to true.
The issue is with the line document.data()[document.data()['ref']].updateData({"Display": true}),. I am not sure how to update the data for the specific icon that has been clicked on.
The error I am getting is:
The method 'updateData' was called on null.
Receiver: null
Tried calling: updateData(_LinkedHashMap len:1)
Any help would be appreciated!

I figured it out following this tutorial.
I replaced onTap: () => document.data()[document.data()['ref']].updateData({"display": true}), with onTap: () => updateData((document.data()['name']), {'display' : true}),
Then added
updateData(selectedDoc, newValues) {
print(selectedDoc);
print(newValues);
FirebaseFirestore.instance
.collection('icons')
.doc(selectedDoc)
.update(newValues)
.catchError((e) {
print(e);
});
}

Related

Flutter Cubit Testing States

Trying to write tests for a cubit in flutter. When I run my code, everything works fine, but the tests are causing me issues.
When I run this, I get an error:
> type 'Null' is not a subtype of type 'Future<Fact>'
> MockFactRepository.fetchFact
/test/fact_cubit_test.dart
group(
'Get Fact',
() {
const fact = Fact(text: 'This is a fact');
blocTest(
'emits [FactCubit.initial, .loading, .loaded] when successful',
build: () {
when(mockFactRepository.fetchFact()).thenAnswer((_) async => fact);
return FactCubit(mockFactRepository);
},
act: (FactCubit cubit) => cubit.getFact(),
expect: () => [
FactState.initial().runtimeType,
FactState.loading().runtimeType,
FactState.loaded(fact).runtimeType,
],
);
},
);

Error Message: A nullable expression can't be used as a condition

How to fix this. Error message A nullable expression can't be used as a condition.
Try checking that the value isn't 'null' before using it as a condition.
This is my code.
floatingActionButton: FloatingActionButton(
onPressed: () => _webcamVideoElement.srcObject.active ? _webcamVideoElement.play() : _webcamVideoElement.pause(),
tooltip: 'Start stream, stop stream',
child: Icon(Icons.camera_alt),
),
);
}
Try this:
onPressed: () => _webcamVideoElement?.srcObject?.active ? _webcamVideoElement.play() : _webcamVideoElement.pause(),
It's pretty simple, before each property that can be null you should check null, try to click "Quick Fix" with Ctrl+. (Mac Command+.) to see what is suggested and that will probably fix it.
Also you didn't showed all your code, so I only can guess you are dealing with (VideoElement)[https://api.flutter.dev/flutter/dart-html/VideoElement-class.html] in that case your srcObject can (be null)[https://api.flutter.dev/flutter/dart-html/MediaElement/srcObject.html] in this case pretty close to what #Mohammad said, I suggest you to try this:
floatingActionButton: FloatingActionButton(
onPressed: () => _webcamVideoElement.srcObject?.active ? _webcamVideoElement.play() : _webcamVideoElement.pause(),
tooltip: 'Start stream, stop stream',
child: Icon(Icons.camera_alt),
),
);
}

How to add a navigation to another screen in a list in flutter

I have made a dart file where I will pull static data and call them in another dart file. Those data need a navigation to another screen but I cannot find a way to add the navigation.
Here is the list without path:
"Delivery",
"Transfer",
"Consumption",
"Adjustment",
];```
and the result I want to achieve is something like this:
```List tasks = [
{'label': 'Delivery',
'path': ' Navigator.pushNamed(context, '/deliveryScreen');'
},
{'label': 'Transfer',
'path': ' Navigator.pushNamed(context, '/transferScreen');'
},
];```
Is there a way of doing it? Cheers
You're on the right track. You could just add the name of the path to the corresponding page widget you want to navigate to, like:
List tasks = [
{ 'label': 'Delivery', 'path': '/deliveryScreen' },
{ 'label': 'Transfer', 'path': '/transferScreen' }
];
Each one of them will correspond to your routes (if you're doing it like this on your MaterialApp routes):
...
routes: {
'/deliveryScreen': (context) => const DeliveryScreen(),
'/transferScreen': (context) => const TransferScreen()
}
...
Then is all a matter of you accessing that tasks lists wherever you'll be rendering the navigation strategy you need (i.e. you want to do a Row* of TextButton widgets, as in):
Row(
children: List.generate(tasks.length, (index) {
var task = tasks[index];
return TextButton(
onPressed: () {
Navigator.of(context).pushNamed(task['path']));
},
child: Text(task['label'])
);
})
)

how to refresh current page while doing pull to refresh (LiquidPullToRefresh) in Flutter Bloc

am using bloc method for home screen. I want to do pull to refresh in home screen...
I tired too many ways not getting solution...could you please suggest some solution.
In side onrefresh() I added few changes
1 . BlocProvider(
create: (context) => OttGetAllMovie(httpClient: http.Client())
..add(FeedsFetched(widget.title, "homePage")),
//child: HomePage(),
child: OttGetHomeInnerCatPage(
wtitle: widget.title, mixpanelinner: widget.mixpanel),
)
2. _feedsBloc.add(FeedsFetched(widget.wtitle, "homePage"));
3. setState(() {
_feedsBloc.add(FeedsFetched(widget.wtitle, "homePage"));
BlocProvider(
create: (context) => OttGetAllMovie(httpClient: http.Client())
..add(FeedsFetched(widget.title, "homePage")),
//child: HomePage(),
child: OttGetHomeInnerCatPage(
wtitle: widget.title, mixpanelinner: widget.mixpanel),
)
});
While
this seems to be the method you're using to fetch data
OttGetAllMovie(httpClient: http.Client())
..add(FeedsFetched(widget.title, "homePage")),
Wrap your body in liquid pull to refresh
LiquidPullToRefresh(
key: _refreshIndicatorKey, // key if you want to add
onRefresh: ()async{
//cal your data source
OttGetAllMovie(httpClient: http.Client())
..add(FeedsFetched(widget.title, "homePage")),
}
child: ListView(), // scroll view
);

Flutter "Rate my App" always requires a Review when using native dialog on Android

I'm using the 'Rate my App' package to ask for rates in my app. https://pub.dev/packages/rate_my_app
I'm using the same code shown in the package page, but i'm setting the 'ignoreNativeDialog' parameter to false in order to use the native API even in Android. But when the dialog appears on android always requires a review after a rate.
Is it any way to set the review to be optional?
RateMyApp rateMyApp = RateMyApp(
preferencesPrefix: 'rateMyApp_',
minDays: 7,
minLaunches: 10,
remindDays: 7,
remindLaunches: 10,
);
rateMyApp.init().then((_) {
if (rateMyApp.shouldOpenDialog) {
rateMyApp.showStarRateDialog(
context,
title: 'Rate this app',
message: 'You like this app ? Then take a little bit of your time to leave a rating :',
actionsBuilder: (context, stars) {
return [
FlatButton(
child: Text('OK'),
onPressed: () async {
print('Thanks for the ' + (stars == null ? '0' : stars.round().toString()) + ' star(s) !');
await rateMyApp.callEvent(RateMyAppEventType.rateButtonPressed);
Navigator.pop<RateMyAppDialogButton>(context, RateMyAppDialogButton.rate);
},
),
];
},
ignoreNativeDialog: false, // This is the one parameter i set to false
dialogStyle: DialogStyle(
titleAlign: TextAlign.center,
messageAlign: TextAlign.center,
messagePadding: EdgeInsets.only(bottom: 20),
),
starRatingOptions: StarRatingOptions(),
onDismissed: () => rateMyApp.callEvent(RateMyAppEventType.laterButtonPressed),
);
}
});
Now it's clear.
The review was required becouse I was using it on a registered tester account. I tested with another account and it worked fine.