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

时间:2020-06-29 06:35:59

标签: json flutter listview widget

当我尝试将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小部件中,它也显示错误为

“字符串”类型不是“小部件”类型的子类型

请更正此错误,请先谢谢〜。

2 个答案:

答案 0 :(得分:2)

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

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

工作演示

enter image description here 完整代码

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(),
    );
  }
}

答案 1 :(得分:0)

我不确定答案是否正确,只是我的猜测,通过查看以上答案,我认为我错过了main.dart页面中的一件事,即,

themeData(       
 visualDensity: VisualDensity.adaptivePlatformDensity,
)

飘飘!