当我连接到flutter搜索栏时,没有看到任何数据。它在代码中没有任何错误

时间:2019-08-28 11:57:20

标签: flutter dart

由于抖动搜索实现错误,无法检索json数据。 D / eglCodecCommon(21638):setVertexArrayObject:将vao设置为0(0)1 0 “出现这种情况时,我会看到一个搜索框,但无法搜索。我该如何选择 执行吗?我将在下面编写代码。请帮助我

enter image description here

main.dart

import 'package:flutter/material.dart';
import 'package:flutter_app11/model.dart';
import 'package:rxdart/rxdart.dart';
import 'package:http/http.dart' as http;

import 'dart:convert';

const key = '';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Movie Searcher",
      theme: ThemeData.dark(),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  HomeState createState() => HomeState();
}

class HomeState extends State<HomePage> {
  List<Movie> movies = List();
  bool hasLoaded = true;

  final PublishSubject subject = PublishSubject<String>();

  @override
  void dispose() {
    subject.close();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    subject.stream;
  }

  void searchMovies(query) {
    resetMovies();
    if (query.isEmpty) {
      setState(() {
        hasLoaded = true;
      });
      return; //Forgot to add in the tutorial <- leaves function if there is no query in the box.
    }
    setState(() => hasLoaded = false);
    http
        .get(
        'https://dapi.kakao.com/v3/search/book?sort=accuracy&target=title&size=10&query= $query',
        headers: {"Accept": "application/json" , 'Authorization' : '5a1167ba90ccb4b79d84ef5a41da0fbd'})
        .then((res) => (res.body))
        .then(json.decode)
        .then((map) => map["results"])
        .then((movies) => movies.forEach(addMovie))
        .catchError(onError)
        .then((e) {
      setState(() {
        hasLoaded = true;
      });
    });
  }

  void onError(dynamic d) {
    setState(() {
      hasLoaded = true;
    });
  }

  void addMovie(item) {
    setState(() {
      movies.add(Movie.fromJson(item));
    });
    print('${movies.map((m) => m.title)}');
  }

  void resetMovies() {
    setState(() => movies.clear());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Movie Searcher'),
      ),
      body: Container(
        padding: EdgeInsets.all(10.0),
        child: Column(
          children: <Widget>[
            TextField(
              onChanged: (String string) => (subject.add(string)),
            ),
            hasLoaded ? Container() : CircularProgressIndicator(),
            Expanded(
                child: ListView.builder(
                  padding: EdgeInsets.all(10.0),
                  itemCount: movies.length,
                  itemBuilder: (BuildContext context, int index) {
                    return new MovieView(movies[index]);
                  },
                ))
          ],
        ),
      ),
    );
  }
}

class MovieView extends StatefulWidget {
  MovieView(this.movie);
  final Movie movie;

  @override
  MovieViewState createState() => MovieViewState();
}

class MovieViewState extends State<MovieView> {
  Movie movieState;

  @override
  void initState() {
    super.initState();
    movieState = widget.movie;
  }

  @override
  Widget build(BuildContext context) {
    return Card(
        child: Container(
            height: 200.0,
            padding: EdgeInsets.all(10.0),
            child: Row(
              children: <Widget>[
                movieState.thumbnail != null
                    ? Hero(
                  child: Image.network(
                      "https://image.tmdb.org/t/p/w92${movieState.thumbnail}"),
                  tag: movieState.publisher,
                )
                    : Container(),
                Expanded(
                    child: Stack(
                      children: <Widget>[
                        Align(
                          alignment: Alignment.center,
                          child: Padding(
                            padding: EdgeInsets.all(10.0),
                            child: Text(
                              movieState.title,
                              maxLines: 10,
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.topRight,
                          child: IconButton(
                            icon: movieState.favored
                                ? Icon(Icons.star)
                                : Icon(Icons.star_border),
                            color: Colors.white,
                            onPressed: () {},
                          ),
                        ),
                        Align(
                          alignment: Alignment.bottomRight,
                          child: IconButton(
                            icon: Icon(Icons.arrow_downward),
                            color: Colors.white,
                            onPressed: () {},
                          ),
                        )
                      ],
                    ))
              ],
            )));
  }

}

model.dart

import 'package:meta/meta.dart';

class Movie {
  Movie({
    @required this.title,
    @required this.authors,
    @required this.publisher,
    @required this.thumbnail,
    this.favored,
  });

  String title, authors, publisher, thumbnail;
  bool favored;

  Movie.fromJson(Map json)
      : title = json["title"],
        authors = json["authors"],
        publisher = json["publisher"].toString(),
        thumbnail = json["thumbnail"],
        favored = false;
}

0 个答案:

没有答案