class MyAppState extends State<MyApp> {
  List data;
  String _search = 'nature';
  int index = 0;
  File imageFile;
  String imageData;
  bool dataLoaded;
  var path;
  int count = 10;
  FlutterShakePlugin _shakePlugin;
  void initState() {
    super.initState();
    _shakePlugin = FlutterShakePlugin(
      onPhoneShaken: () {
        setState(() {
      count=count+10;
    });
  },
      },
    )..startListening();
  }

  void dispose() {
    super.dispose();
    _shakePlugin.stopListening();
  }

  Future<String> getjsondata() async {
    try {
      var response = await http.get(
          'https://api.unsplash.com/search/photos?per_page=${count}&client_id=TcAQEO3JoMG90U7Rl-YUiDo1x9XbZukzMOMQhxUVCV4&query=${_search}');
      setState(() {
        var converted = json.decode(response.body);
        data = converted['results'];
      });
    } catch (e) {}
    return 'success';
  }
  void saveImage(int i) async {
    var url = data[i]['urls']['small'].toString();

    var imageId = await ImageDownloader.downloadImage(url);
    path = await ImageDownloader.findPath(imageId);
  }

  @override
  Widget build(BuildContext context) {
    getjsondata();
    return GestureDetector(
      child: SwipeDetector(
        child: Container(
          child: Image.network(
            data[index]['urls']['small'],

最佳答案

getjsondata方法中调用build方法将导致ui无限渲染,因为您正在setState中调用getjsondata。我认为震动插件可以正常工作,但结果却是无效的,因为屏幕处于无限渲染状态。
如果将getjsondata移至 FutureBuilder ,请从setState方法内部删除getjsondata调用,并根据Future的结果呈现您的用户界面,您的代码应能正常工作。

10-08 12:33