当前位置:优学网  >  在线题库

RangeError(索引):无效值:有效值范围为空:0失败的断言:行'url!=空':不是真的

发表时间:2022-07-22 00:17:41 阅读:187

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 >"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

🎖️ 优质答案
  • 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,
          ),
  • 相关问题