Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RangeError (index): Invalid value: Valid value range is empty: 0 Failed assertion: line 'url != null': is not true

I’m facing this error in Image.network I don’t get why I get the message

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

And from current snippet

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"error: The argument type ‘Widget’ can’t be assigned to the parameter type ‘String’. "

import 'package:flutter/material.dart';
import 'post_model.dart';

class SearchPage extends StatefulWidget {
  final List<Post> posts;

  const SearchPage({required this.posts});

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  List<Post> _searchedPost = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: TextField(
          style: const TextStyle(color: Colors.white),
          decoration: const InputDecoration(
              hintText: 'Cerca Articolo',
              hintStyle: TextStyle(color: Colors.white),
              border: InputBorder.none),
          onChanged: (val) {
            setState(() {
              _searchedPost =
                  widget.posts.where((el) => el.title.contains(val)).toList();
            });
          },
        ),
      ),
      body: _searchedPost.isEmpty
          ? Center(
              child: Text(
                'Nessun articolo disponibile',
                //   //snapshot.data!= null ? snapshot.data![i]["_embedded"]["wp:featuredmedia"][0]["source_url"] : "Nessun articolo",
                style: Theme.of(context).textTheme.headline3,
              ),
            )
          : ListView.builder(
              itemCount: _searchedPost.length,
              itemBuilder: (context, i) {
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Card(
                      margin: const EdgeInsets.all(10),
                      elevation: 5,
                      shadowColor: Colors.black26,
                      color: Colors.white,
                      child: InkWell(
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(10),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Image.network(_searchedPost[i].urlImage == null ? const Text("could not found") : Image.network(""),
                                 width: double.infinity,
                                 height: 220,
                                 fit: BoxFit.cover,
                              ),
                              // Title article
                              Column(
                                children: [
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        left: 16, top: 16, bottom: 16),
                                    child: Row(
                                      children: [
                                        Expanded(
                                          child: Text(_searchedPost[i].title,
                                            maxLines: 3,
                                            overflow: TextOverflow.clip,
                                            softWrap: true,
                                            style: const TextStyle(
                                              fontSize: 18,
                                              fontWeight: FontWeight.w600,
                                              fontFamily: "Raleway",
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          // Navigator.push(
                          // context,
                          // MaterialPageRoute(
                          // builder: (context) =>
                          //   ArticlePage(data: snapshot.data?[i]),
                          // ),
                          // );
                        },
                      ),
                    ),
                    const Divider(
                      height: 1,
                    )
                    // ListTile(
                    //   title: Text(_searchedPost[i].title),
                    // ),
                  ],
                );
              },
            ),
    );
  }
}

Error place

>Solution :

Do like this for image

_searchedPost[i].urlImage == null
    ? const Text("could not found")
    : Image.network(
        _searchedPost[i].urlImage,
        width: double.infinity,
        height: 220,
        fit: BoxFit.cover,
      ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading