benjamindean / flutter_vibration

Handle vibration on iOS and Android in Flutter apps
229 stars 69 forks source link

HasVibrator does not work #16

Closed ghost closed 4 years ago

ghost commented 4 years ago

My visual code is causing an error when I put this method (has) in my code. Can you give me a hand?


import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';

void main() => runApp(Home());

class Home extends StatelessWidget {
  const Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Flutter",
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.red,
          title: Text("Flutter vibrate"),
        ),
        body: Body(),
      ),
    );
  }
}

class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  void vibrate() {
    if(Vibration.hasVibrator()){
      Vibration.vibrate();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: FlatButton(
        onPressed: vibrate,
        child: Text("Click to vibrate"),
      )
    );
  }
}`
benjamindean commented 4 years ago

@IL7Games What is the error exactly?

ghost commented 4 years ago

Compiler message: lib/main.dart:35:18: Error: A value of type 'Future' can't be assigned to a variable of type 'bool'.

  • 'Future' is from 'dart:async'. Try changing the type of the left hand side, or casting the right hand side to 'bool'. if(Vibration.hasVibrator()){ ^ Compiler failed on /home/***/Documentos/Dev/Flutter/flutter_flutter/lib/main.dart
  • Where: Script '/home/***/.development/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 765

  • What went wrong: Execution failed for task ':app:compileFlutterBuildDebugArm'. Process 'command '/home/***/.development/flutter/bin/flutter'' finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 37sGradle task assembleDebug failed with exit code 1

ghost commented 4 years ago

HasVibrate uses the Future class, so it has to use async to work:

void vibrate() async {
 if (await Vibration.hasVibrator()){
   //Vibration.vibrate();
   Vibration.vibrate(duration: 100);
  }
}

"Thank you" for your absence benjamindean.

karolzlot commented 4 years ago

"Thank you" for your absence benjamindean.

@IL7Games This is rude. You didn't pay benjamindean for his time

ghost commented 4 years ago

Yes, I was a little rude and I apologize for that @benjamindean.

You didn't pay benjamindean for his time

I know I didn't pay him anything but who made the plugin @qqgg231 ? I know he's not obligated to help me, but couldn't he give me just a little bit of help?

Anyway, I hope that with this topic can help other people with the same problem and once again I'm sorry benjamindean.