Noop primitive operations - flutter

I recently upgraded my flutter to 3.7 and I got this error all over my project
Noop primitive operations
ListView.builder(
key: Key('builder ${selected.toString()}'), //attention
padding: const EdgeInsets.only(top: 22),
itemCount: widget.faq.length,
itemBuilder: (final BuildContext context, final int index) => listItems(widget.faq[index], index),
)
The error is on toString method.

Possibly a linter error indicating that the call toString is unnecessary. You are already interpolating - have you considered removing toString()

Related

The operator '[]' isn't defined for the class 'Iterable<dynamic>' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
[!Here is the code where i got an error, i could not use [] for this code
return ListView.builder(
reverse: true,
itemCount: chatDocs?.length,
itemBuilder: (ctx, index) => MessageBubble(
chatDocs[index].data()['text'],
chatDocs[index].data()['username'],
chatDocs[index].data()['userImage'],
chatDocs[index].data()['userId'] == user.uid,
),
// Container(
// padding: const EdgeInsets.all(8),
// child: Text(chatDocs?[index]['text']),)
);e ][1]][1]
I also tried null check and 'as Map' to chat-Docs, But both are didnot work
add .toList() in chatDocs declaration line
so it becomes like this:
final chatDocs = chatSnapshot.data?.docs.toList();
.map returns an Iterable which doesn't have the [] operator. You can solve this by turning it into a list by calling toList() on it. So
final chatDocs = chatSnapshot.data?.docs.map(docs).toList();
But it looks like your map() is already wrong. Note the error there. Do you really need to map it? If not then this already should work
final chatDocs = chatSnapshot.data?.docs;
Or actually this to handle it in case it's null
final chatDocs = chatSnapshot.data?.docs ?? [];
You can use this way,
StreamBuilder(
stream: _yoursteam,
builder: ((context, AsyncSnapshot snapshot) {
return snapshot.hasData
? Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
return MsgTile(
snapshot.data.docs[index]["msgId"],
snapshot.data.docs[index]["time"],
snapshot.data.docs[index]["message"],
snapshot.data.docs[index]["sender"],
FirebaseAuth.instance.currentUser!.uid ==
snapshot.data.docs[index]["uid"],
);
},
),
)
: Container();
}),
);

Why does it throw an Unhandled Exception here?

I'm trying to persist my calendar data with hive but when I start the program, it throws this:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'bool' in type cast
This is my model class:
#HiveType(typeId: 1)
class Event extends HiveObject {
#HiveField(0)
late String title;
#HiveField(1)
late DateTime eventDateTime;
#HiveField(2)
bool wholeDayEvent;
Event({
required this.title,
required this.eventDateTime,
required this.wholeDayEvent,
});
#override
String toString() => title;
}
And here's the build method of my displaying widget:
Expanded(
child: ValueListenableBuilder<Box<Event>>(
valueListenable: Boxes.getEvents().listenable(),
builder: (context, box, _) {
final events = box.values.toList().cast<Event>();
return ListView.builder(
itemCount: events.length,
itemBuilder: (context, index) {
return Container(
margin: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 4.0,
),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(12.0),
),
child: ListTile(
onTap: () => print('${box.values}'),
title: Text('${box.values}'),
),
);
},
);
},
),
),
I can assume that you changed some fields in your Event object, while your Hive database is not empty, so the previously stored objects do not have those new fields so their values are null, which throws the error.
You can fix this by simply clearing your Hive box before using it, so it deletes the previous data objects and work with the new one, in your main():
await Hive.openBox<Event>("yourBoxName"); // opens the box
await Hive.box<Event>("yourBoxName").clear(); // clear the box
Now you will have an empty box, and your code will work fine again.
you should just clear it one time to remove previous data, after that you could remove the clear() line and work normally.

Why I am getting type model - dynamic error on dynamic T type generic?

I have three classes,
1.UiInfiniteScroller : my statefulWidget class (classic stateful widget),
InfiniteScrollerViewModel : my view model class,and its contains an object as widget and widget is like;
final UiInfiniteScrollerWidgets<T> widgets;
UiInfiniteScrollerWidgets widgets has a function like;
final Widget Function(BuildContext context, T item) widgetBuilder;
now my problem is on itemBuilder function;
return ListView.builder(
padding: widget.padding ?? EdgeInsets.zero,
physics: widget.physics ?? const BouncingScrollPhysics(),
shrinkWrap: widget.shrinkWrap ?? false,
itemBuilder: (BuildContext context, int index) {
return widget.vM.widgets.widgetBuilder(context, widget.vM.paginationService.results[index]);
},
itemCount: widget.vM.paginationService.results.length + _additionalWidgetCount,
);
if I call this function I am getting error like
type '(BuildContext, XxModel) => SizedBox' is not a subtype of type '(BuildContext, dynamic) => Widget',
and xx Model is my T model.
if add this code
print("isType equal to T:" + (widget.vM.paginationService.results[index] is T).toString());, it is returning true, so my list is T type.
And My widget builder function is;
widgetBuilder: (BuildContext context, MockResultModel item) {
return SizedBox(
height: 35.h,
width: double.infinity,
child: Padding(
padding: const EdgeInsets.only(left: 30.0),
child: UiLexend(
text: "Item Id:${item.id} Searched Keyword: ${item.keyword}",
),
),
);
}
if I change the below line,
final Widget Function(BuildContext context, T item) widgetBuilder;
with;
final Widget Function(BuildContext context, dynamic item) widgetBuilder;
it is working correctly but if item type is T it is failing.
So why it is happening, I mean I am giving all classes the same Model type and is T are returning true but for builder function, it is giving the error :/
can anyone help me?

Invalid value: Not in inclusive range error, But my listview.builder has itemcount property

return Container(
padding: EdgeInsets.all(10),
height: MediaQuery.of(context).size.height * 0.7,
child: ListView.builder(
shrinkWrap: true,
itemCount: lotteries.length,
itemBuilder: (BuildContext ctx, int index) {
var lottery =
json.decode(lotteries[index].lotteryNumbers);
print(lottery);
return ListView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemCount: lottery.length,
itemBuilder: (BuildContext ctex, int i) {
return GetBuilder<CartController>(
init: CartController(),
builder: (context) {
return new GestureDetector(
onTap: () {
cartController.addItem(lottery[i]);
lotteryController
.toggleFavouriteStatus(i);
},
child: Container(Text("${lottery[i]}"))
I have two listviews but both of them have item count property. I am getting the not-in-inclusive range error. But I have specified the length for both my lists. Any help would be much appreciated.
This error occurs when you run out of values when iterating over an array or list. In the case of the ListView component missing the itemCount prop, the component attempts to continue to iterate but is unaware when to complete so it eventually continues on out of range (the length of the array).
You could also see this error after running a poorly set up for loop. For example:
var arr = [1, 2, 3, 4];
for (var i=0; i < 5; i++) {
print(arr[i]);
}
This code would result in a range error as well. The array has 4 items yet we attempt to iterate 5 times.
you can print itemCount: lotteries.length & itemCount: lottery.length
if evrything is ok u can wait some delay in to call second lattery listbuilder.

Flutter Hive: deleteAt is making the value null instead of deleting it

So let's say I have a list of books.
Deleting book at nth index using deleteAt is not actually deleting it and shifting (n+1)th element to it's place, rather it is making it null. i.e. the element at nth is now null.
How to perform deleteAt perfectly?
BTW I have used delete outside of the ValueListenableBuilder.
ValueListenableBuilder(
valueListenable: Hive.box('books').listenable(),
builder: (context, box, _) {
if (box.values.length == 0)
return Center(
child: Text("No books"),
);
return ListView.builder(
primary: true,
padding: EdgeInsets.only(bottom: 95),
itemCount: box.values.length,
itemBuilder: (context, int index) {
Book book = box.get(index);
return Padding(
padding:
const EdgeInsets.only(bottom: kMasterPadding),
child: BookItem(
title: book.title,
author: book.authorName,
),
);
},
);
},
),
deleting code
() async {
await Hive.box("books").deleteAt(Hive.box("books").length - 2);
//deleted at last 2nd coz deleting at the end was working perfectly
},
You can try Book book = box.getAt(index); instead of Book book = box.get(index);
https://github.com/hivedb/hive/issues/376
You can remove an element from a List<E> by running .removeAt(index) on your list. This also shifts your last elements one index to the left.