代码之家  ›  专栏  ›  技术社区  ›  Husna

分析flutter中WordPress自定义post类型的JSON数据

  •  1
  • Husna  · 技术社区  · 6 年前

    我使用flutter处理移动应用程序。在这里我想访问WordPress的自定义post-type滑条在颤振。我试着喜欢这个

     import 'dart:async';
     import 'dart:convert';
    
     import 'package:flutter/material.dart';
     import 'package:http/http.dart' as http;
    
     void main() async {
       List _jsonData = await getJson();
       print(_jsonData);
           runApp(new MaterialApp(
              home: new Scaffold(
               appBar: new AppBar(
               title: new Text('Demo'),
               centerTitle: true,
               backgroundColor: Colors.redAccent,
            ),
             body: new Column(
                  children: <Widget>[
                  new Text(_jsonData)
              ],
               ),
              ),
              ));
            }
    
    
           Future<List<Map<String,dynamic>>> getJson() async {
           String apiUrl = 'http://bannermonster.com/demo.json';
           http.Response response = await http.get(apiUrl);
           return json.decode(response.body);
           }
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Yamin    6 年前

    我认为问题在于代码中的URL拼写错误。然而,完整的例子如下:

    import 'dart:async';
    import 'dart:convert';
    
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    
    void main() {
      runApp(new MaterialApp(
        home: MainScreen(),
      ));
    }
    
    class MainScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
            future: getJson(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                return Container(
                  child: new Scaffold(
                    appBar: new AppBar(
                      title: new Text('Demo'),
                      centerTitle: true,
                      backgroundColor: Colors.redAccent,
                    ),
                    body: new Column(
                      children: <Widget>[new Text(snapshot.data[0]['id'].toString())],
                    ),
                  ),
                );
              } else if(snapshot.hasError) {
                return Container(
                  child: new Scaffold(
                    appBar: new AppBar(
                      title: new Text('Demo'),
                      centerTitle: true,
                      backgroundColor: Colors.redAccent,
                    ),
                    body: new Column(
                      children: <Widget>[new Text(snapshot.error.toString())], //Handle error in your own way
                    ),
                  ),
                );
              }else{
                return Scaffold(
                  body: Container(
                    child: Center(child: CircularProgressIndicator()),
                  ),
                );
              }
            });
      }
    }
    
    Future<List<dynamic>> getJson() async {
      try {
        String apiUrl = 'http://bannersmonster.com/demo.json';
        http.Response response = await http.get(apiUrl);
        return json.decode(response.body);
      } catch (e) {
        throw Exception('Problem with data'+e.toString());
      }
    }
    

    注: