【问题标题】:Parse JSON data from WordPress Custom post types in flutter在颤振中解析来自 WordPress 自定义帖子类型的 JSON 数据
【发布时间】:2019-01-19 22:36:00
【问题描述】:

我正在使用 Flutter 处理移动应用程序。在这里,我想在 Flutter 中访问 WordPress 自定义帖子类型滑块。我试着喜欢这个

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

我的问题是我应该如何拉动 WordPress 自定义帖子类型在颤振完整的滑块数据中如何拉动。我刚开始学习颤振,谁能指出我正确的方向。这是我的代码,我没有收到任何错误。

【问题讨论】:

  • 你会在你的问题中放一个 JSON 示例吗?
  • @Yamin 我添加了我的代码。
  • 它在哪里?我没有看到 JSON 示例。
  • @Yamin apiurl 这里我有一个 json WP rest API。
  • 如何获取数据示例?您只将路线放在那里,而不是实际的 URL。更新您的问题并举一个 WP JSON 响应的示例。

标签: android json wordpress dart flutter


【解决方案1】:

我认为问题在于代码中的 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());
  }
}

注意:上面的代码是这个问题的简单指南。不要将它用于其他porpuses。

【讨论】:

  • 我试过了,但没有得到,也没有显示错误。我更新了我的问题检查。
  • 谢谢。是的,一个领域即将到来。但在这里我想获取完整的 json 对象。我试过这样print(_jsonData); 但没有得到。
  • 试试 print(snapshot.data.toString());在 "if (snapshot.hasData) {" 之后。我举了一个例子。你应该写其他部分。
  • Dart 没有未定义的!把实际的错误放在这里。
  • 控制台错误Error -32601 received from application method not found这个错误我得到了。
猜你喜欢
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多