Search code examples
flutterdartgoogle-mapsproviderstate-management

void call to provider class gives error that a widget must be retuned what should i do


I am new to flutter and i am learning google maps i want to animate camera but i have written its function in provider class, provider provides googlemap because its return type is widget but to animate camera i made a void function and the issue is that in displayscreen i am unable to use a function which donot retun a widget(or is void) furthermore if i call it by making an object of the provider class ,no animation takesplace in ui screen is there any idea to use avoid function from provider class

this is ui screen

import 'package:flutter/material.dart';
import 'package:mapsingoogle/providers/secondprovider/secondprovider.dart';
import 'package:mapsingoogle/widgets/appbar/myappbar.dart';
import 'package:mapsingoogle/widgets/mybutton.dart';
import 'package:provider/provider.dart';

class SecondProviderScreen extends StatefulWidget {
  const SecondProviderScreen({super.key});

  @override
  State<SecondProviderScreen> createState() => _SecondProviderScreenState();
}

class _SecondProviderScreenState extends State<SecondProviderScreen> {
  var prov = SecondProvider();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(title: 'Second Provider', actions: []),
      body: SafeArea(
        child: Column(
          children: [
            Container(
              width: 400,
              height: 600,
              color: Colors.orangeAccent,
              child: Consumer<SecondProvider>(
                builder: (context, value, child) => value.mymap(),
              ),
            ),
            MyButton(
                ontap: () {
                  Consumer<SecondProvider>(
                    builder: (context, value, child) {
                      value
                          .animate(); //here error is a non null value must be returned
                    },
                  );
                },
                title: 'Ánimate')
          ],
        ),
      ),
    );
  }
}


and here is the provider class

`import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:mapsingoogle/components/cameraposition/cameraposition.dart';
import 'package:mapsingoogle/components/completer/completer.dart';
import 'package:mapsingoogle/components/makers/markers.dart';



class SecondProvider with ChangeNotifier{


  CameraPosition cameraPosition=MyCamera.Position;//another class
  CameraPosition newposition=MyCamera.australiaposition;//another class
  Completer<GoogleMapController> completer=Completer();

  GoogleMap mymap(){
    return GoogleMap(initialCameraPosition: cameraPosition,
    markers: Set<Marker>.of(MyMarkers.funckantamarker()),
      onMapCreated: (controller) =>completer.complete(controller),
    );
  }//this function works properly

   void  animate()async{
    GoogleMapController controller=await completer.future;
    controller.animateCamera(CameraUpdate.newCameraPosition(MyCamera.australiaposition));
    notifyListeners();
  }//how i can use this function in Consumer or in floatingactionbutton


}`




Solution

  • I found a solution by using the provider variable:

     as   Widget build(BuildContext context) {
        final secondprovider=Provider.of<SecondProvider>(context, listen: false); 
    

    and then used it to call the function.