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

Type 'String' is not a subtype of type 'Widget' in FLUTTER

这个错误是在我尝试将json object包裹在Text widget中时出现的,代码如下

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 object 并尝试在 [=20] 上的列表视图中显示标题 object =] 虽然我包裹在文本小部件中,但它显示错误,

type 'String' is not a subtype of type 'Widget'

请修复此错误,提前致谢~。我是 flutter 的新手

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

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

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

themeData(       
 visualDensity: VisualDensity.adaptivePlatformDensity,
)

飘飘快乐!