为什么这个抖动功能在 flutter 中不起作用?

Why this shake functionality not working in flutter?

This ShakePlugin is not working with this piece of code,when im just using this code without these api calls and all its working fine.

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'],

I want to increase the count of images i recieve from api on shake of screen but this is not working even if i have installed all the libraries and all.

build 方法中调用您的 getjsondata 方法将导致 ui 无限渲染,因为您在 getjsondata 中调用 setState。我认为 shake 插件工作正常,但它的结果是无效的,因为屏幕处于无限渲染状态。

如果您将 getjsondata 移动到 FutureBuilder,请从 getjsondata 方法内部移除 setState 调用并在 ui 的结果上呈现Future 你的代码应该可以工作。