Home > Software design >  how to return value from onTap function in flutter
how to return value from onTap function in flutter

Time:10-03

I am new to programming and flutter. I am facing an error while returning a value from the onTap function. The error says The return type 'int' isn't a 'void', as required by the closure's context. I am getting images from API and showing them on a screen. I want that when someone clicks on an image, the image id should be stored in a variable that will be used on another screen. I am using Gesturedetector's onTap function to get the image id and I am successful to print that id into the terminal and store it in a variable but I am unable to return that variable and want to use that variable on another screen. Anyone, please help me to return the value of that variable. My code is below

import 'package:flutter/material.dart';
import 'Service.dart';
import 'View2.dart';
import 'models/memegenetor_model.dart';

class template_view extends StatefulWidget {
  template_view({Key? key}) : super(key: key);

  @override
  State<template_view> createState() => _template_viewState();
}

class _template_viewState extends State<template_view> {
  late Future<Memes_Model> futureScore;

  @override
  void initState() {
    super.initState();
    futureScore = ApiService().GetTemplates();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: FutureBuilder<Memes_Model>(
        future: futureScore,
        builder: (context, snapshot) {
          if (snapshot.data != null) {
            return ListView.builder(
                itemCount: snapshot.data!.data.memes.length,
                itemBuilder: (BuildContext context, int index) {
                  return Padding(
                    padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
                    child: Container(
                      color: Colors.red,
                      width: MediaQuery.of(context).size.width,
                        //height: 100,
                        child: GestureDetector(
                          onTap:  (){
                            int tempid=snapshot.data!.data.memes[index].id;
                            return tempid;

                          },
                          child: Column(
                            children: [
                              Image.network(
                                  "${snapshot.data!.data.memes[index].url}",
                                  width: MediaQuery.of(context).size.width * 0.2),

                            ],
                          ),
                        )),
                  );
                });
          } else if (snapshot.hasError) {
            return Text('${snapshot.error}');
          }

          return const CircularProgressIndicator();
        },
      ),
    ));
  }
}

CodePudding user response:

You can't return a value from onTap. Create a new state in your class.

class _template_viewState extends State<template_view> {
  int? tempid = null; // or int tempid = -1;
  
  // ...
}

onTap should look like this:

onTap: () {
  tempid = snapshot.data!.data.memes[index].id; // use setState if require
},
  • Related