Unable to load asset: Image - list

I have this code for asset image
Image.asset(
'${listPic[1]}',
),
the images are located in a List
final List<Image> listPic= <Image>[ Image.asset('assets/images/pic1.jpg'), Image.asset('assets/images/pic2.jpg'), ];
I know the pubsec works, because when I directly type the image location like this
Image.asset(
'assets/images/pic1.jpg',
),
the image is displayed,
Is there something wrong with the list or Image.asset()
I have tried using .toString after the ${listPic[0]} it didn't do anything instead said unnecessary string interpolation.
gives this error
The following assertion was thrown resolving an image codec: Unable to load asset: Image(image: AssetImage(bundle: null, name: "assets/images/pic1.jpg"), frameBuilder: null, loadingBuilder: null, alignment: Alignment.center, this.excludeFromSemantics: false, filterQuality: low)

The list you declared is a List<Image> type, but you are using it as if it's a List<String>.
Try this instead:
final List<String> imagePaths = [
'your/path/pic1.jpg',
'your/path/pic2.jpg',
];
Image.asset(
toolImage[1],
)

Related

tristate || value != null': is not true for a list of CheckBoxListTiles

I am facing this error all of a sudden even when I havn't changed any logic in my code. So apparently the list of CheckBoxListTiles is not being built and this error is being thrown to me. I have no idea why it is being thrown since this is my first time facing this error. Thanks in advance for the help. Also I am attaching the Widget below to which the error is pointing to.
Widget checklistOptions1(String title) {
return CheckboxListTile(
title: Text(
title,
style: Theme.of(context).textTheme.subtitle1,
),
value: values1[title],
onChanged: (isFalse) {
setState(() {
values1[title] = isFalse!;
});
},
activeColor: redAccentColor,
checkboxShape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
),
controlAffinity: ListTileControlAffinity.trailing,
);
}
This is the widget to which the error is pointing to and I dont see anything wrong with it although this widget was working perfectly fine a while ago.
It must mean that values1[title] is null but it needs to be true or false.
Alternatively you indicate tristate: true on the Checkbox. Then null is also allowed

Retrieving information from nested lists (Flutter)

I have what appears to most likely be a simple problem for everyone else, yet for some reason I can't seem to fix. I am a complete Flutter noob, but with coding experience. I am hoping to get tips around structuring data, and I have created a list which houses another list, like so:
class Store{
final String name;
final String image;
final List <Product> products; // this is defined in another class, and it requires a name(string), description(string), price (double)
Store({required this.name, required this.image, required this.features, required this.price})
List <Store> store= [
Store(name: "Ikea:, image: "ikealogo.png",
products :[Product(name: "wood table", description: "a very nice wood table", price: 12.50),
Product(name: "comfy chair", description: "a very nice leather chair", price: 10.50),
Product(name: "awesome lamp", description: "an ultra bright lamp", price: 5.50),]),
Store(name: "Bestbuy:, image: "bestbuylogo.png",
products :[Product(name: "television", description: "a very nice television", price: 350.00),
Product(name: "radio", description: "a very loud radio", price: 15.50),
Product(name: "cellphone", description: "a very small phone", price: 78.50),]),
];
}
Basically I have like 20 more of these things, following the same format.
Now for my problem, I can't seem to create a list out of the "Product" info nested within "Store". I can create lists with Store just fine, and I can call on the name and logo for all parts of the UI. My challenge is getting the Product information after a Store is selected, as the code I use currently shows "Iterable" when I hover over it. On the other hand, I get "List" just fine when I define or call the store list on another route.
#override
Widget build(BuildContext context) {
final product = Store.store.map((store) {
final productlist = store.products.toList();
});
I know that my code may not make any sense, and you can give me any kind of recommendations to alter the data structure altogether. For now (without using databases just yet), I want to show what products are available based on the store selected.
I hope everyone has a safe and productive day. Thank you!
In the example below I have set up a drop down button to show the list of stores. Selection causes a repaint which show the products in the selected store.
Hope the helps.
class _MyHomePageState extends State<MyHomePage> {
int _storeIdx = 0;
Store get _currentStore => storeList[_storeIdx];
List<Store> storeList = [
Store(name: "Ikea:", image: "ikealogo.png", products: [
Product(name: "wood table", description: "a very nice wood table", price: 12.50),
Product(name: "comfy chair", description: "a very nice leather chair", price: 10.50),
Product(name: "awesome lamp", description: "an ultra bright lamp", price: 5.50),
]),
Store(name: "Bestbuy:", image: "bestbuylogo.png", products: [
Product(name: "television", description: "a very nice television", price: 350.00),
Product(name: "radio", description: "a very loud radio", price: 15.50),
Product(name: "cellphone", description: "a very small phone", price: 78.50),
]),
];
#override
Widget build(BuildContext context) {
int _offset = 0;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton(
value: _storeIdx,
items: storeList
.map((Store s) => DropdownMenuItem(
child: Text(s.name),
value: _offset++,
))
.toList(),
onChanged: (int? value) {
setState(() {
_storeIdx = value ?? 0;
});
},
hint: const Text('Select store')),
Text("Currently ${_currentStore.name}"),
..._currentStore.products.map((Product p) => Text(p.name)).toList(),
],
),
),
);
}
}
Note that the spread operator ... is quite useful when you are trying to flatten a hierarchy of widgets.
Also, sometimes in Flutter, there is a tendency to have one humongous build method. Sometimes it is clearer to break a screen in separate widgets or to break the build method into several methods each returning an array of widgets that can then be consolidated in the build method.
I can't help but notice that store inside of Store class is not a static variable which means Store.store should not be accessible. However, if store were a static variable inside Store class Store.store will work.
So,
class Product {
String name;
Product(this.name);
#override
String toString() {
return "Product($name)";
}
}
class Store {
List<Product> products;
Store(this.products);
static List<Store> stores = [
Store([
Product("Soap"),
Product("Bar"),
]),
Store([
Product("Sponge"),
Product("ChocoBar"),
]),
];
}
void main() {
print(Store.stores[0].products);
print(Store.stores[1].products);
}
Will yield an output of :
[Product(Soap), Product(Bar)]
[Product(Sponge), Product(ChocoBar)]
which is what we expect to find.

RangeError (index): Invalid value. Valid value range is empty: 0

Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
password[0] == null ? unchangedPassword : changedPassword, //Error happens here
color: Colors.white,
size: 20,
),
)
]
)
Can someone please explain why I can't write an if statement that checks if the value at the index is equal to a null value?
Use password.isEmpty instead of password[0] == null
password[0] doesn't exist.
It appears password is a List
you could do password[0] == null if it was a map, but it's not. It's a list. That value doesn't exist. It's not null. It doesn't exist. So it blows up.
Why doesn't it exist? Because the memory wasn't allocated. Flutter will allocated the memory automatically when you need it. If you do password.add(value), then password[0] will exist at that point. But since you're getting the range error you haven't used password.add
For lists, use password.isNotEmpty or password.length == 0;
I have been also facing this issue and following checking solved my issue
Text("${(subjects.isEmpty) ? "" : subjects.name}"),

I want to select the icon from the list but it always showing some other icon instead of icon in code point

I am trying to get the icon from the list but the code I am entering into the list is not the same I got from the output
final List<int> points = <int>[
0xe5d5,
58837,
];
final Random r = Random();
Icon randomIcon() => Icon(
IconData(points[r.nextInt(points.length)],
fontFamily: 'MaterialIcons', matchTextDirection: true),
color: myColor,
);
print(r.nextInt(points.length));

How to resolve parent to child relationship with AppSync

I have schema looking like below
type Post {
id: ID!
creator: String!
createdAt: String!
like: Int!
dislike: Int!
frozen: Boolean!
revisions:[PostRevision!]
}
type PostRevision {
id: ID!
post: Post!
content: String!
author: String!
createdAt: String!
}
type Mutation {
createPost(postInput: CreatePostInput!): Post
}
I would like to be able to batch insert Post and PostRevision at the same time when i run createPost mutation; however, VTL is giving me a much of hard time.
I have tried below
## Variable Declarations
#set($postId = $util.autoId())
#set($postList = [])
#set($postRevisionList = [])
#set($post = {})
#set($revision = {})
## Initialize Post object
$util.qr($post.put("creator", $ctx.args.postInput.author))
$util.qr($post.put("id", $postId))
$util.qr($post.put("createdAt", $util.time.nowEpochMilliSeconds()))
$util.qr($post.put("like", 0))
$util.qr($post.put("dislike", 0))
$util.qr($post.put("frozen", false))
## Initialize PostRevision object
$util.qr($revision.put("id", $util.autoId()))
$util.qr($revision.put("author", $ctx.args.postInput.author))
$util.qr($revision.put("post", $postId))
$util.qr($revision.put("content", $ctx.args.postInput.content))
$util.qr($revision.put("createdAt", $util.time.nowEpochMilliSeconds()))
## Listify objects
$postList.add($post)
$postRevisionList.add($revision)
{
"version" : "2018-05-29",
"operation" : "BatchPutItem",
"tables" : {
"WHISPR_DEV_PostTable": $util.toJson($postList),
"WHISPR_DEV_PostRevisionTable": $util.toJson($postRevisionList)
}
}
So basically I am reconstructing the document in the resolver of createPost so that I can add Post then also add ID of the post to postReivision However when I run below code
mutation insertPost{
createPost(postInput:{
creator:"name"
content:"value"
}){
id
}
}
I get following error
{
"data": {
"createPost": null
},
"errors": [
{
"path": [
"createPost"
],
"data": null,
"errorType": "MappingTemplate",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "Expected JSON object but got BOOLEAN instead."
}
]
}
What am I doing wrong?
I know it would be easier to resolve with lambda function but I do not want to double up the cost for no reason. Any help would be greatly appreciated. Thanks!
If anyone still needs the answer for this (this question is still the #1 google hit for the mentioned error message):
The problem is the return value of the add() method, which returns a boolean value.
To fix this, just wrap the add() methods into $util.qr, as you are already doing for the put() methods:
$util.qr(($postList.add($post))
$util.qr(($postRevisionList.add($revision))
It looks like you are missing a call to $util.dynamodb.toDynamoDBJson which is causing AppSync to try to put plain JSON objects into DynamoDB when DynamoDB requires a DynamoDB specific input structure where each attribute instead of being a plain string like "hello world!" is an object { "S": "hello world!" }. The $util.dynamodb.toDynamoDBJson helper handles this for you for convenience. Can you please try adding the toDynamoDBJson() to these lines:
## Listify objects
$postList.add($util.dynamodb.toDynamoDBJson($post))
$postRevisionList.add($util.dynamodb.toDynamoDBJson($revision))
Hope this helps :)