Center to user location google maps initializer error - flutter

I am trying to set a center loading point whenever user enters the app. I am getting the errow with initializer. Not sure how to fix this.
The instance member 'StartingLocation' can't be accessed in an initializer.
class _GmapState extends State<Gmap> {
Location location = new Location();
GoogleMapController? mapController;
late Future<Map> futureAdventures;
var StartingLocation;
var _initialCameraPosition = CameraPosition(target:
LatLng(StartingLocation.latitude, StartingLocation.longitude),
zoom: 15);
#override
void initState() {
super.initState();
var pos = location.getLocation();
StartingLocation = pos;
}
Tried this as well
var pos;
#override
void initState() {
super.initState();
setState(() {
pos = _location.getLocation();
pos = _StartingLocation;
_initialCameraPosition = CameraPosition(target:
LatLng(_StartingLocation.latitude, _StartingLocation.longitude),
zoom: 15);
});

Related

flutter memory leak my widget in homescreen

this is the error cause when I return to home screen. Is there any way to solve this? I tried dispose my timer but it did not work
bool containerClicked = false;
int nextSpinValue = 0;
int? widgetIndex = 0;
var spinController = StreamController<int>.broadcast();
void spin() => spinController.add(++nextSpinValue)
#override
void initState() {
Redirects.drawerList();
super.initState();
}
#override
void dispose() {
_timer!.cancel();
super.dispose();
}
#override
Widget build(BuildContext context) {
var mHeight = MediaQuery.of(context).size.height;
var mWidth = MediaQuery.of(context).size.width;
final _formKey = GlobalKey<FormState>();
var spinController = StreamController<int>.broadcast();
int nextSpinValue = 0;
void spin() => spinController.add(++nextSpinValue);
Timer.periodic(const Duration(seconds: 3), (timer) async {
if (nextSpinValue >= 3) {
nextSpinValue = 0;
}
spin();
});
caused somewhere in setstate
The problem happens due to the fact that StreamController has to be closed. Otherwise, StreamController object stays in the memory even if the widget getting stream events no longer exists. dispose() is a good place to call the close method.
AFAIK it's a property of Flutter Controllers. You have to close them.

Update touch position (slide) during time with Flame (Flutter)

in my game with Flame I have a space ship, and I want it to follow where I'm pressing with my finger. For the moment it goes to the first spot I touch until I leave the finger from the screen, but if I move keeping it on, it continues to follow where I initially pressed. Is there a way to update where my finger is(using HasTappableComponents)? Or should I change approach?
class SpaceFlame extends FlameGame with HasTappableComponents {
late Ship ship;
late TapHandler tapHandler;
#override
Future<void> onLoad() async {
ship = Ship()
..position = Vector2(size.x / 2, size.y / 2)
..size = Vector2(64, 64)
..setSpeed(100);
tapHandler = TapHandler();
ship.anchor = Anchor.center;
await addAll([
Background(Palette.background),
ship,
tapHandler,
]);
}
#override
void render(Canvas canvas) {
canvas.drawRect(canvasSize.toRect(), Paint()..color = Palette.buttonBlu);
}
#override
void update(double dt) {
// TODO: implement update
if (tapHandler.tapped) {
print('tapped');
Vector2 moveDirection = Vector2(
tapHandler.tapPosition.x - ship.position.x,
tapHandler.tapPosition.y - ship.position.y);
ship.setMoveDirection(moveDirection);
} else {
ship.setMoveDirection(Vector2.zero());
}
super.update(dt);
}
}
class TapHandler extends PositionComponent with TapCallbacks {
TapHandler() : super(anchor: Anchor.center);
bool tapped = false;
Vector2 _tapPosition = Vector2.zero();
final _paint = Paint()..color = const Color(0x00000000);
#override
void onGameResize(Vector2 canvasSize) {
super.onGameResize(canvasSize);
size = canvasSize;
position = canvasSize / 2;
}
Vector2 get tapPosition => _tapPosition;
#override
void render(Canvas canvas) {
canvas.drawRect(size.toRect(), _paint);
}
#override
void onTapDown(TapDownEvent event) {
print('object');
tapped = true;
_tapPosition = event.canvasPosition;
}
}
When you keep your finger on the screen and drag it the even is no longer a tap, but a drag.
To react to this event you need to use HasDraggableComponents on the game and DragCallbacks on the component. Then you need to implement onDragUpdate in your component and set the position to the position that the event reports for the drag.

How to implement Audioplayers with audioservice in flutter? using offline playlist

I want to make an audio app in flutter Having audioplayers and audioservice package in flutter.
how to implement them please help me.
here is my code:
class _PlayerScreenState extends State
with SingleTickerProviderStateMixing, Observer {
_PlayerScreenState(
#required this.songArray, this.songIndex, this.songArrayFiles);
var songArray = [];
var songArrayFiles = [];
var songIndex = 0;
var autoplay = "Autoplay On";
double volume = 1;
bool playing = false;
Duration _duration = const Duration();
Duration _position = const Duration();
late AudioPlayer advancedPlayer;
late AudioCache audioCache;
late AnimationController _controller;
final Color color = HexColor.fromHex('#F30C4E');
//INIT STATE;
#override
void initState() {
super.initState();
initPlayer();
}
autoplaysong() {
advancedPlayer.stop();
songIndex = songIndex;
playSongs(songArray[songIndex]);
}
void initPlayer() {
advancedPlayer = AudioPlayer();
audioCache = AudioCache(fixedPlayer: advancedPlayer);
advancedPlayer.onDurationChanged.listen(
(d) => setState(() => _duration = d),
);
advancedPlayer.onPlayerCompletion.listen((event) {
if (autoplay == "Autoplay On") {
autoplaysong();
} else {
nextsong();
}
});
advancedPlayer.onAudioPositionChanged
.listen((p) => setState(() => _position = p));
playSongs(songArray[songIndex]);
}

How to detect tap on Flame components?

I am using Flutter and flame (0.29.3) to build a simple mobile game. I am trying to detect taps on PositionComponent/SpriteComponent. However, I fail to do so. References/tutorials are using addGestureRecognizer but it is deprecated.
I define my PositionComponent is as follows;
class Enemy extends PositionComponent with Tapable {
Rect enemyRect;
Enemy(double x, double y) {
enemyRect = Rect.fromLTWH(x, y, 50, 50);
}
#override
void render(Canvas c) {
Color color = Color(0XFFFF0000);
Paint enemyColor = Paint()..color = color;
c.drawRect(enemyRect, enemyColor);
}
void update(double t) {}
#override
void onTapUp(TapUpDetails details) {
print("tap up");
}
#override
void onTapDown(TapDownDetails details) {
print("tap down");
}
#override
void onTapCancel() {
print("tap cancel");
}
}
And, I add PositionComponent to my game.
class GameController extends BaseGame with HasTapableComponents {
Size screenSize;
double tileSize;
Player player;
Enemy enemy;
TapableComponent a;
GameController() {
initialize();
}
void initialize() async {
resize(await Flame.util.initialDimensions());
add(enemy = Enemy(200, 200));
}
#override
void render(Canvas c) {
Rect background = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
Paint backgroundPaint = Paint()..color = Color(0xFFFAFAFA);
enemy.render(c);
}
#override
void update(double t) {}
#override
void resize(Size size) {
screenSize = size;
tileSize = screenSize.width / 10;
}
}
However, it's not working, am I missing something?
I think your example code is a mix between v1 code and 0.29.3, if you try with the latest release candidate of Flame: 1.0.0-rc8 then the following should work:
class TapableSquare extends PositionComponent with Tapable {
static final Paint _white = Paint()..color = const Color(0xFFFFFFFF);
static final Paint _grey = Paint()..color = const Color(0xFFA5A5A5);
TapableSquare({Vector2 position})
: super(
position: position ?? Vector2.all(100),
size: Vector2.all(100),
);
#override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(size.toRect());
}
#override
bool onTapUp(TapUpDetails details) {...}
#override
bool onTapDown(TapDownDetails details) {...}
#override
bool onTapCancel() {...}
}
class TapablesGame extends BaseGame with HasTapableComponents {
#override
Future<void> onLoad() async {
add(TapableSquare());
}
}
Extracted from this example.
EDIT: To make it work for 0.29.3 you should be setting position and size in your Enemy class instead of building your own rect, Tapable can't know about that rect.

Why is onTap method not working in flame?

I am trying to recreate the game flappy birds using flame and flutter but I encountered a problem and I was wondering if anyone would be able to help me.
The problem is that the onTap method is not working. This is my code:
const COLOR = const Color(0xFF75DA8B);
const SIZE = 52.0;
const GRAVITY = 400.0;
const BOOST = -380.0;
void main() async{
WidgetsFlutterBinding.ensureInitialized ();
final size = await Flame.util.initialDimensions();
final game = MyGame(size);
runApp(game.widget);
}
class Bg extends Component with Resizable{
static final Paint _paint = Paint()..color = COLOR;
#override
void render(Canvas c) {
c.drawRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height), _paint);
}
#override
void update(double t) {
}
}
class Bird extends AnimationComponent with Resizable{
double speedY = 0.0;
bool frozen;
Bird () : super.sequenced(SIZE, SIZE, 'bird.png', 4, textureWidth: 16.0, textureHeight: 16.0) {
this.anchor = Anchor.center;
}
Position get velocity => Position (300.0, speedY);
reset () {
this.x = size.width/ 2;
this.y = size.height/2;
speedY = 0;
frozen = true;
angle = 0.0;
}
#override
void resize(Size size) {
super.resize(size);
reset();
}
#override
void update(double t) {
super.update(t);
if (frozen) return;
this.y += speedY * t - GRAVITY * t * t / 2;
this.speedY += GRAVITY * t;
this.angle = velocity.angle();
if (y > size.height) {
reset();
}
}
onTap () {
if (frozen) {
frozen = false;
return;
}
speedY = (speedY + BOOST).clamp(BOOST, speedY);
}
}
class MyGame extends BaseGame {
Bird bird;
MyGame (Size size){
add(Bg());
add(bird = Bird());
}
#override
void onTap() {
bird.onTap();
}
}
The bird stays static, and if I comment the line of code:
if (frozen) return;
on the update method, then it falls but ontap is not working.
Would you know why?
Thank you so much in advance.
I don't know which version of Flame you were using since this is quite an old question. If you are using a release candidate of 1.0.0 today you should at least follow the following structure:
class MyGame extends BaseGame with HasTapableComponents {
Future<void> onLoad() async {
// Load images and sprites etc
add(MyComponent());
}
... your game code
}
class MyComponent extends SpriteAnimationComponent with Tapable {
...
#override
bool onTapUp(TapUpInfo event) {}
#override
bool onTapDown(TapDownInfo event) {}
#override
bool onTapCancel() {}
}