From 2e2de5bbc0b533268602741723dd5114fda73da1 Mon Sep 17 00:00:00 2001 From: SamuelMauricioL <67238016+SamuelMauricioL@users.noreply.github.com> Date: Mon, 16 Aug 2021 23:03:58 -0500 Subject: [PATCH] Migrating package to null-safety --- example/main.dart | 8 +++++--- lib/draw_particles.dart | 22 +++++++++++----------- lib/particle.dart | 26 ++++++++++++++------------ lib/particles.dart | 6 +++--- pubspec.yaml | 2 +- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/example/main.dart b/example/main.dart index ea14b44..d9a2ede 100644 --- a/example/main.dart +++ b/example/main.dart @@ -1,3 +1,5 @@ +// ignore_for_file: unused_element + import 'package:flutter/material.dart'; import 'package:flutter_particles/particles.dart'; @@ -26,7 +28,7 @@ class MyApp extends StatelessWidget { } class MyHomePage extends StatefulWidget { - MyHomePage({Key key, this.title}) : super(key: key); + MyHomePage({Key? key, this.title}) : super(key: key); // This widget is the home page of your application. It is stateful, meaning // that it has a State object (defined below) that contains fields that affect @@ -37,7 +39,7 @@ class MyHomePage extends StatefulWidget { // used by the build method of the State. Fields in a Widget subclass are // always marked "final". - final String title; + final String? title; @override _MyHomePageStateCustom createState() => new _MyHomePageStateCustom(); @@ -72,4 +74,4 @@ class _MyHomePageStateCustom extends State { ), ); } -} \ No newline at end of file +} diff --git a/lib/draw_particles.dart b/lib/draw_particles.dart index 8bfd121..5c1ffcf 100644 --- a/lib/draw_particles.dart +++ b/lib/draw_particles.dart @@ -2,9 +2,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_particles/particle.dart'; class DisplayPoints extends CustomPainter { - final List particlesList; + final List? particlesList; - final double strokeSize; + final double? strokeSize; DisplayPoints({ this.particlesList, @@ -15,26 +15,26 @@ class DisplayPoints extends CustomPainter { void paint(Canvas canvas, Size size) { Paint line = new Paint(); line.strokeCap = StrokeCap.round; - line.color = particlesList.elementAt(0).color; + line.color = particlesList!.elementAt(0).color!; // Draw all the particles - for (var particle in particlesList) { - line.strokeWidth = particle.size; + for (var particle in particlesList!) { + line.strokeWidth = particle.size!; Offset center = new Offset(particle.xCoor, particle.yCoor); line.style = PaintingStyle.fill; - canvas.drawCircle(center, particle.size, line); + canvas.drawCircle(center, particle.size!, line); } // Draw the connect line - for (int i = 0; i < this.particlesList.length; i++) { - for (int j = i + 1; j < this.particlesList.length; j++) { - Particle particle = this.particlesList.elementAt(i); - Particle anotherParticle = this.particlesList.elementAt(j); + for (int i = 0; i < this.particlesList!.length; i++) { + for (int j = i + 1; j < this.particlesList!.length; j++) { + Particle particle = this.particlesList!.elementAt(i); + Particle anotherParticle = this.particlesList!.elementAt(j); if (particle.isNear(anotherParticle)) { Offset firstParticle = new Offset(particle.xCoor, particle.yCoor); Offset secondParticle = new Offset(anotherParticle.xCoor, anotherParticle.yCoor); - line.strokeWidth = strokeSize; + line.strokeWidth = strokeSize!; canvas.drawLine(firstParticle, secondParticle, line); } } diff --git a/lib/particle.dart b/lib/particle.dart index 0cf5ee9..defea2b 100644 --- a/lib/particle.dart +++ b/lib/particle.dart @@ -1,16 +1,18 @@ +// ignore_for_file: unnecessary_statements + import 'dart:math'; import 'package:flutter/material.dart'; class Particle { - final Color color; - double xCoor; - double yCoor; - final double size; - double xDirection; - double yDirection; - static double widgetWidth; - static double widgetHeight; + final Color? color; + final double yCoor; + final double xCoor; + final double? size; + late double xDirection; + late double yDirection; + static late double widgetWidth; + static late double widgetHeight; double connectDistance; Random random = new Random(); double minSpeed; @@ -18,8 +20,8 @@ class Particle { Particle({ this.color, - this.xCoor, - this.yCoor, + required this.xCoor, + required this.yCoor, this.size, this.connectDistance = 100.0, this.minSpeed = 0.1, @@ -54,8 +56,8 @@ class Particle { this.yCoor + this.yDirection < 0) { this.yDirection = this.yDirection * (-1); } - this.xCoor += this.xDirection; - this.yCoor += this.yDirection; + this.xCoor + this.xDirection; + this.yCoor + this.yDirection; } double doubleInRange(double start, double end) => diff --git a/lib/particles.dart b/lib/particles.dart index 1c74d96..60d4e53 100644 --- a/lib/particles.dart +++ b/lib/particles.dart @@ -27,11 +27,11 @@ class Particles extends StatefulWidget { class ParticlesState extends State with SingleTickerProviderStateMixin { - Animation animation; - AnimationController animationController; + Animation? animation; + late AnimationController animationController; Random random = new Random(); - List particlesList = new List(); + List particlesList = []; void addToParticlesList() { for (int i = 1; i <= widget.numParticle; i++) { diff --git a/pubspec.yaml b/pubspec.yaml index 08bc993..133c880 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,7 +11,7 @@ homepage: https://github.com/Aleadinglight/Flutter-Particles version: 1.0.0+8 environment: - sdk: ">=2.0.0-dev.68.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: