skip to Main Content

I’m coding a flutter video game here.I have tried to test it on the web and it worked fine but when doing it on my physical device(phone) an error encountered on brick.dart file.

As a beginner I would like to get help from you guys:

import 'package:flutter/material.dart';

class MyBrick extends StatelessWidget {
  final x;
  final y;

  MyBrick({this.x, this.y});

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment(x, y),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: Container(
          color: Colors.white,
          height: 20,
          width: MediaQuery.of(context).size.width / 5,
        ),
      ),
    );
  }
}

2

Answers


  1. The flutter Alignment(double x, double y) widget takes doubles. Since you are passing x and y to your widget without specifying their types, if you pass the values x = 1 and y = 3 this alignment widget will throw that error. Update your code to include your field types. this will force you to pass x and y of appropriate types or you will get a linter error:

    import 'package:flutter/material.dart';
    
    class MyBrick extends StatelessWidget {
      final double x; // type your fields
      final double y; // type your fields
    
      MyBrick({this.x, this.y});
    
      @override
      Widget build(BuildContext context) {
        return Container(
          alignment: Alignment(x, y), // accepts only doubles so if you pass an int value, you will get that error
          child: ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Container(
              color: Colors.white,
              height: 20,
              width: MediaQuery.of(context).size.width / 5,
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. because Alignment widget expects double values but you’re passing int values.

    alignment: Alignment(x.toDouble(), y.toDouble()), // convert int to double
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search