Home > Enterprise >  "FlutterError: Unable to load asset" while adding image from network
"FlutterError: Unable to load asset" while adding image from network

Time:01-02

I was trying to add some leading image into a List View inside of some Stream Build structure. The problem is whatever I tried i always got the same error. I make some research in Stackoverflow but all solutions i could find was about local images, not images from internet. So, I don't know how to implement those solutions into my project.

Widget build(BuildContext context) {
     final Stream<QuerySnapshot> _stuffStream = FirebaseFirestore.instance
    .collection('Stuffs') 
    .orderBy('dateTime',
        descending: true) 
    .snapshots();

    return StreamBuilder<QuerySnapshot>(
    stream: _stuffStream,
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.hasError) {
        return Text('Please try again');
      }

      if (snapshot.connectionState == ConnectionState.waiting) {
        return Text("Please wait");
      }

      return ListView(
        children: snapshot.data!.docs.map((DocumentSnapshot document) {
          Map<String, dynamic> data =
              document.data()! as Map<String, dynamic>;
          return ListTile(
            leading: CircleAvatar(
              backgroundImage: AssetImage(
                  "https://www.kaspersky.com.tr/content/tr-tr/images/smb/icons/icon-ksos.png"),
            ),
            title: Text(data['title']),
            subtitle: Column(
              children: <Widget>[
                Text(data['details']),
                TextButton(
                    child: const Text('Send message'),
                    onPressed: () {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => ChatPage(docs: data)));
                    }),
                TextButton.icon(
                    icon: Icon(Icons.favorite),
                    label: Text('Favorite'),
                    onPressed: () async {
                      favorite(data['stuffID']);
                    }),
              ],
            ),
          );
        }).toList(),
      );
    });
  }
}

I also tried to use this but I got the same error:

leading: SizedBox(
                    height: 100.0,
                    width: 100.0, // fixed width and height
                    child: Image.asset(data['stuffImage']))

I also add this this permissions to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

CodePudding user response:

Change asset image to network image

backgroundImage: NetworkImage(
              "https://www.kaspersky.com.tr/content/tr-tr/images/smb/icons/icon-ksos.png"),
        ),
  • Related