liodali / osm_flutter

OpenStreetMap plugin for flutter
https://pub.dev/packages/flutter_osm_plugin
MIT License
229 stars 95 forks source link

Can't add OSMMixinObserver #201

Closed jesussmile closed 2 years ago

jesussmile commented 2 years ago

Hi, with your help, I was able to fix the rider part of my app but as I try to do the same thing with the Driver part I can't seem to add the interface OSMMixinObserver , I get the error classes can only mix in mixin and classes Here is my code

import 'dart:async';

import 'package:drivers_app/AllScreens/registrationScreen.dart';
import 'package:drivers_app/Assistants/assistantMethod.dart';
import 'package:drivers_app/DataHandler/appData.dart';
import 'package:drivers_app/Models/drivers.dart';
import 'package:drivers_app/Notifications/pushNotificationService.dart';
import 'package:drivers_app/configMaps.dart';
import 'package:drivers_app/main.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter_osm_plugin/flutter_osm_plugin.dart';

import 'package:geolocator/geolocator.dart';
import 'package:flutter_geofire/flutter_geofire.dart';

import 'package:background_location/background_location.dart';
import 'package:provider/provider.dart';

class HomeTabPage extends StatefulWidget {
  //const HomeTabPage({ Key? key }) : super(key: key);

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

class _HomeTabPageState extends State<HomeTabPage>
    with AutomaticKeepAliveClientMixin, OSMMixinObserver {
  String driverStatusText = "Go Online";
  Color driverStatusColor = Colors.black;
  bool isDriverAvailable = false;

  //Geoflutterfire geo = Geoflutterfire();
  MapController osmController;
  GeoPoint initPosition;
  final GlobalKey _mapKey = GlobalKey();
  Position initPositionPoint;
  // GeoFirePoint point;

  GlobalKey<ScaffoldState> scaffoldKey;
  var geoLocator = Geolocator();
  //Position currentPosition;

  void locatePosition() async {
    initPositionPoint = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    //print("This is your Position:: " + initPositionPoint.toString());

    GeoPoint initPos = GeoPoint(
        latitude: initPositionPoint.latitude,
        longitude: initPositionPoint.longitude);
    initPosition = initPos;
    currentPosition = initPos;
    String address = await AssistantMethods.searchCoordinateAddress(
        initPositionPoint, context);
  }

  @override
  void initState() {
    super.initState();

    getCurrentDriverInfo();
    osmController = MapController(
      initMapWithUserPosition: true,
    );

    osmController.addObserver(this);
    scaffoldKey = GlobalKey<ScaffoldState>();

    locatePosition();
  }

  @override
  Future<void> mapIsReady(bool isReady) async {
    if (isReady) {
      await osmController.rotateMapCamera(45.0);
      await osmController.currentLocation();
      await osmController.rotateMapCamera(0);
      locatePosition();
    }
  }

  Future<void> getCurrentDriverInfo() async {
    doublenotification++;
    currentFirebaseUser = FirebaseAuth.instance.currentUser;
    driversRef
        .child(currentFirebaseUser.uid)
        .once()
        .then((DataSnapshot dataSnapshot) {
      if (dataSnapshot.value != null) {
        driversInformation = Drivers.fromSnapshot(dataSnapshot);
      }
    });
    PushNotificationService pushNotificationService = PushNotificationService();

    pushNotificationService.initialize(context);
    pushNotificationService.getToken();
    AssistantMethods.retrieveHistoryInfo(context);

  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Stack(
      children: [
        OSMFlutter(
          key: _mapKey,
          controller: osmController,
          trackMyPosition: true,
          initZoom: 18,
          minZoomLevel: 8,
          maxZoomLevel: 18,
          stepZoom: 1.0,
          userLocationMarker: UserLocationMaker(
            personMarker: MarkerIcon(
              image: AssetImage("images/images/car-ios.png"),
            ),
            directionArrowMarker: MarkerIcon(
              image: AssetImage("images/images/car-ios.png"),
            ),
          ),
          road: Road(
            startIcon: MarkerIcon(
              icon: Icon(
                Icons.person,
                size: 64,
                color: Colors.brown,
              ),
            ),
            roadColor: Colors.yellowAccent,
          ),
          markerOption: MarkerOption(
              defaultMarker: MarkerIcon(
            icon: Icon(
              Icons.star_half_rounded,
              color: Colors.blue,
              size: 56,
            ),
          )),
        ),
        Container(
          height: 140.0,
          width: double.infinity,
          color: Colors.black54,
        ),
        Positioned(
          top: 60.0,
          left: 0.0,
          right: 0.0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.0),
                child: Consumer<AppData>(
                  builder: (ctx, dsc, child) => (RaisedButton(
                    color: dsc.driverStatusClr,
                    onPressed: () {
                      if (isDriverAvailable != true) {
                        makeDriverOnlineNow();
                        getLocationLiveUpdates();
                        Provider.of<AppData>(context, listen: false)
                            .buttonOnline(Colors.green, "Online");

                        isDriverAvailable = true;
                        // });
                        displayToastMessage("You are Online now", context);
                      } else {
                        Provider.of<AppData>(context, listen: false)
                            .buttonOnline(Colors.black, "Offline");
                        driverOffline();

                        displayToastMessage("You are Offline now", context);
                        isDriverAvailable = false;
                      }
                    },
                    child: Padding(
                      padding: EdgeInsets.all(17),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            dsc.driverStatusTxt,
                            style: TextStyle(
                                fontSize: 20.0,
                                fontWeight: FontWeight.bold,
                                color: Colors.white),
                          ),
                          Icon(
                            Icons.phone_android,
                            color: Colors.white,
                            size: 26,
                          )
                        ],
                      ),
                    ),
                  )),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  void makeDriverOnlineNow() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);

    await Geofire.initialize("availableDrivers");

    await Geofire.setLocation(
        currentFirebaseUser.uid, position.latitude, position.longitude);

    await rideRequestRef.set("searching");
    rideRequestRef.onValue.listen((event) {});
  }

  void getLocationLiveUpdates() async {
    hometabPageStreamSubscription =
        Geolocator.getPositionStream().listen((Position position) {
      initPositionPoint = position;
      if (isDriverAvailable == true) {
        Geofire.setLocation(
            currentFirebaseUser.uid, position.latitude, position.longitude);
      }
    });

    await BackgroundLocation.startLocationService();
    backgroundSubscription = BackgroundLocation.getLocationUpdates((location) {
      print(location.latitude);
      print(location.longitude);

      BackgroundLocation.setAndroidNotification(
        title: "Location",
        message:
            location.latitude.toString() + " " + location.longitude.toString(),
        icon: "@mipmap/ic_launcher",
      );
    });
  }

  void driverOffline() async {
    Geofire.removeLocation(currentFirebaseUser.uid);
    rideRequestRef.onDisconnect();

    await rideRequestRef.remove();
    //rideRequestRef = null;
  }

  Future<void> initGeoFireListener() async {
    var driverLocation = FirebaseDatabase(databaseURL: firebaseUrl)
        .reference()
        .child("availableDrivers")
        .path;

    Geofire.initialize(driverLocation);
    //Geofire.initialize("availableDrivers");
    Geofire.queryAtLocation(initPosition.latitude, initPosition.longitude, 50)
        .listen((map) {
      //  print("DriversLocationPath $map");
      if (map != null) {
        var callBack = map['callBack'];

        switch (callBack) {
          case Geofire.onKeyEntered:
            // keysRetrieved.add(map["key"]);
            break;

          case Geofire.onKeyExited:
            //  keysRetrieved.remove(map["key"]);
            break;

          case Geofire.onKeyMoved:
            // Update your key's location
            break;

          case Geofire.onGeoQueryReady:
            // All Intial Data is loaded
            //  print(map['result']);

            break;
        }
      }

      setState(() {});
    });

    //comment
  }

  @override
  bool get wantKeepAlive => true;
}

pubspec.yaml


environment:
  sdk: ">=2.7.0 <3.0.0"
#dependency_overrides:
#  flutter_osm_interface: "<=0.1.5"
dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.

  cupertino_icons: ^1.0.2
  firebase_auth: ^1.1.4
  firebase_core: ^1.1.1
  firebase_database: ^6.1.2
  flutter_osm_plugin: ^0.26.1
  flutter_typeahead: ^3.1.3
  fluttertoast: ^8.0.6
  geolocator: ^7.0.3

  http: ^0.13.3
  provider: ^5.0.0
  sizer: ^2.0.13
  flutter_keyboard_visibility: ^5.0.2
  animated_text_kit: ^4.2.1
  firebase_storage: ^9.0.0

  flutter_geofire: ^2.0.1
  firebase_messaging: ^10.0.4
  flutter_local_notifications: ^6.0.0
  onesignal_flutter: ^3.2.5
  assets_audio_player: ^3.0.3+4
  maps_toolkit: ^2.0.0
  intl: ^0.17.0
  smooth_star_rating: ^1.1.1
  lint: ^1.5.3
  curved_navigation_bar: ^1.0.1
  background_location: ^0.8.1

dev_dependencies:
  flutter_test:
    sdk: flutter
liodali commented 2 years ago

can you provide me the log to understand more the problem ?

jesussmile commented 2 years ago

Okay, this is a screenshot, of the error that's happening, osmmixinobserver If this is not enough then maybe I can strip down my code, The problem is there is red squiggly lines under OSMMixinObserver and .addObserver(this);

liodali commented 2 years ago

try to run flutter clean & flutter pub get

liodali commented 2 years ago

try to delete flutter_osm_interface of the version 0.1.5 from dart cache and change the version of flutter_osm_interface in pubspec.lock to 0.1.10+3