I tried making a class then making a list of that class and I get an Erorr
the class I created:
class Transaction {
final String id;
final String title;
final double amount;
final DateTime date;
Transaction({
required this.id,
required this.title,
required this.amount,
required this.date,
});
}
Erorrs I get :
Try changing your variable name as it is clashing with the class name Transaction
and you can also use a different approach:
final List<Transaction>? transactionList;
trnasactionList.add(Transaction(id:1,title:'flutter' ....));
trnasactionList.add(Transaction(id:2,title:'flutter' ....));
Related
class Lessons {
final int? id;
final String? title;
final List<Resources> resources;
Lessons({
required this.id,
required this.title,
required this.resources,
});
}
class Resources extends Equatable {
final int? id;
final String? question;
final String? answer;
bool? bookmark;
Resources({
required this.id,
required this.question,
required this.answer,
required this.bookmark,
});
}
-these are my models, and have saved them using the secured storage flutter. How do i change just the bookmark and save it to secured storage?
i was able to change the bookmark but was unable to save it dynamically to secured storage.
hello I am new to flutter and programming, if anyone can help me this is my question
I have one class with multiple products and I need to verify if they are in BD. So this is the class of products:
class CartItem {
final String name;
final double price;
final double quantity;
final String image;
final String cor;
final String tamanho;
CartItem({
required this.name,
required this.price,
required this.quantity,
required this.image,
required this.cor,
required this.tamanho,
});
}
And this is the URL to receive a true or false value depending on whether the product is in the BD or not
http://tzev.ddns/PTTC.1/PTTCProd?Stock=True&Reference=&Color=&Size=
First I have to store the value of the reference in the class, but then I don't know how to read one by one to receive the various values of the API call
I want to keep null as a default value for product id, as it'll be later auto-generated, but I can't due to the flutter null safety check.
this is the instance creation code where I want to keep id = null:
var _editedProduct = Product(
id: null,
title: '',
price: 0,
description: '',
imageUrl: '',
);
and here is the code for my Product.dart file:
import 'package:flutter/material.dart';
class Product with ChangeNotifier {
final String id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavorite;
Product({
required this.id,
required this.title,
required this.description,
required this.price,
required this.imageUrl,
this.isFavorite = false,
});
void toggleFavoriteStatus() {
isFavorite = !isFavorite;
notifyListeners();
}
}
A screenshot of the error
While you like to have id default value as null, First you need to accept nullable data. For final String id; will be final String? id; the default value can be provided on constructor by removing required to act as optional. id will have null by default.
To use id you need to check if it is null or not.
If you are sure it contains data use ! at the end like id!, or
provide a default value like id??"default", or
if(Product.id!=null){...}
class Product with ChangeNotifier {
final String? id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavorite;
Product({
this.id,
required this.title,
required this.description,
required this.price,
required this.imageUrl,
this.isFavorite = false,
});
...
Learn more about null-safety
Following changes solved my issue:
changing final String id; to final String? id; in Product.dart
changing final productId = ModalRoute.of(context)!.settings.arguments as String; to final String? productId = ModalRoute.of(context)!.settings.arguments as String?;
import 'package:flutter/foundation.dart';
enum Complexity {
Simple,
Challenging,
Hard,
}
class Meal {
final String id;
final List<String> categories;
final String title;
final String imageUrl;
final List<String> ingredients;
final List<String> steps;
final int duration;
final Complexity complexity;
final bool isGlutenFree;
final bool isLactoseFree;
final bool isVegan;
final bool isVegetarian;
const Meal({
required this.id,
required this.categories,
required this.title,
required this.imageUrl,
required this.ingredients,
required this.steps,
required this.duration,
required this.complexity,
required this.isGlutenFree,
required this.isLactoseFree,
required this.isVegan,
required this.isVegetarian,
});
}
Hallo, i have added the complexity of my meals to my meal app.
But, how can I add the number of persons? What do I need? I want to add, that the meal is for 1 or 2 or 3 persons.
Thank you!
I am following a Flutter tutorial that has such a following code, but code doesn't work on my computer, and I don't know how to fix it:
import 'package:flutter/foundation.dart';
class CartItem {
final String id;
CartItem({
#required this.id,
});
}
But I get such these errors:
The parameter 'id' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter
{String id}
The latest dart version now supports sound null safety. The tutorial must be using an old version.
To indicate that a variable might have the value null, just add ? to its type declaration:
class CartItem {
final String? id = null;
CartItem({
this.id,
});
}
or
class CartItem {
final String? id;
CartItem({
this.id,
});
}
You have a few options depending on your own project...
Option1: Make id nullable, you can keep #required or remove it.
class CartItem {
final String? id;
CartItem({
this.id,
});
}
Option2: Give a default (non-null) value to id
class CartItem {
final String id;
CartItem({
this.id="",
});
}
more in this link
You can just replace #required this.id with required this.id.
class City {
int id;
String name;
String imageUrl;
bool isPopular;
City(
{required this.id,
required this.name,
required this.imageUrl,
required this.isPopular});
}