【问题标题】:Type 'String' is not a subtype of type 'Widget' in FLUTTER“String”类型不是 FLUTTER 中“Widget”类型的子类型
【发布时间】:2020-10-19 06:47:15
【问题描述】:

当我尝试将 json 对象包装在 Text 小部件中时出现此错误,代码如下

import 'package:flutter/material.dart';
import '../drawer.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class Homepage extends StatefulWidget {
  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  var url = "http://jsonplaceholder.typicode.com/photos";
  var data;
  // Used to initialize something before starting of the screen
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    fetchdata();
  }

  
  fetchdata() async {
    var res = await http.get(url);
    // print(res.body);

    // json parsing
    data = jsonDecode(res.body);
    print(data);

    //To tell the UI that we got the data
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    //Scaffold has prebuild some widget themes
    return Scaffold(
      backgroundColor: Colors.green[100],
      appBar: AppBar(
        title: Text("My App"),
      ),

      // Container is similiar to <Div>
      body: data != null
          
          ? ListView.builder(itemBuilder: (context, index) {
              return ListTile(
                title: Text(data[index]["title"]),
              );
            })

如您所见,我收到了一个 HTTP 响应并将其转换为 json 对象,并尝试按需在 Listview 中显示标题对象。即使我包裹在 Text 小部件中,它也会显示错误,

类型 'String' 不是类型 'Widget' 的子类型

请修复这个错误,谢谢提前~。我是新来的颤振

【问题讨论】:

  • 如果data == null,你在三元条件下返回什么而不是 ListView?
  • 建议您使用 FutureBuilder 来初始化您的未来,并在未来完成时显示数据
  • 嗨@JRamos29,如果数据== null,仅使用不确定的循环进度指示器小部件
  • 嗨@OmerGamliel,对不起,我是新手,如果你不介意,你能更新我上面的代码并解释一下吗?

标签: json flutter listview widget


【解决方案1】:

我不确定它的正确答案,它只是我的猜测,通过看到上面的答案我以为我错过了 main.dart 页面中的一件事,那就是,

themeData(       
 visualDensity: VisualDensity.adaptivePlatformDensity,
)

快乐飘飘!

【讨论】:

    【解决方案2】:

    您可以在下面复制粘贴运行完整代码
    检查data != null 并在数据为null 时返回CircularProgressIndicator()
    代码sn-p

    body: data != null
                ? ListView.builder(itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(data[index]["title"]),
                    );
                  })
                : Center(child: CircularProgressIndicator()))
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    import 'dart:convert';
    
    class Homepage extends StatefulWidget {
      @override
      _HomepageState createState() => _HomepageState();
    }
    
    class _HomepageState extends State<Homepage> {
      var url = "http://jsonplaceholder.typicode.com/photos";
      var data;
      // Used to initialize something before starting of the screen
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        fetchdata();
      }
    
      fetchdata() async {
        var res = await http.get(url);
        // print(res.body);
    
        // json parsing
        data = jsonDecode(res.body);
        print(data);
    
        //To tell the UI that we got the data
        setState(() {});
      }
    
      @override
      Widget build(BuildContext context) {
        //Scaffold has prebuild some widget themes
        return Scaffold(
            backgroundColor: Colors.green[100],
            appBar: AppBar(
              title: Text("My App"),
            ),
            body: data != null
                ? ListView.builder(itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(data[index]["title"]),
                    );
                  })
                : Center(child: CircularProgressIndicator()));
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: Homepage(),
        );
      }
    }
    

    【讨论】:

    • 我真的很想知道,如果没有itemCount for ListView.builder()..???
    • 嗨@srikanth7785,我认为itemcount是可选的,我使用了索引na,它已经足够了
    • 嘿@chun,非常感谢你救了我,即使我的代码和你的相同,我错过了 VisualDensity:VisualDensity.adaptivePlatformDensity,在我的 main.dart 中
    猜你喜欢
    • 2020-10-28
    • 2020-04-07
    • 2019-08-18
    • 2021-05-17
    • 2023-03-27
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多