Skip to content

Instantly share code, notes, and snippets.

View marcel-ploch's full-sized avatar

Marcel Ploch marcel-ploch

View GitHub Profile
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
@marcel-ploch
marcel-ploch / main.dart
Created November 23, 2022 22:24
Singletons in Dart sind doch so einfach
class MeinSingleton {
/// Unsere Singleton Instanz, die einmalig instanziiert wird
static final MeinSingleton _singleton = MeinSingleton._internal();
/// Member Variable, die zugreifbar gemacht werden soll
late List<dynamic> data = [];
/// Factory Constructor, der die Instanz der Klasse zurückgibt, egal wann sie aufgerufen wird
factory MeinSingleton() {
return _singleton;
@marcel-ploch
marcel-ploch / main.dart
Created November 23, 2022 22:03
RichTextExample
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
@marcel-ploch
marcel-ploch / main.dart
Created November 21, 2022 11:01
Klassen schreiben. Konstruktoren und Parameter
class MeinObjektPositionsParameter {
String name = "";
// Positions Parameter an Stelle 1
MeinObjektPositionsParameter(this.name);
}
class MeinObjektOptionalParameterDefault {
String name = "";
// Positions Parameter an Stelle 1, aber optional mit default value
MeinObjektOptionalParameterDefault([this.name = 'FOO']) ;
@marcel-ploch
marcel-ploch / main.dart
Last active November 21, 2022 10:38
Basis Typen Deklaration
class User {
String name;
String type;
User({required this.name, required this.type});
}
enum MyEnum {
foo("test", "admin"),
bar("test2", "user");
Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
await for (final value in stream) {
sum += value;
}
return sum;
}
Stream<int> countStream(int to) async* {
for (int i = 1; i <= to; i++) {
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// Erweitere die Klasse string um die NumberParsing Extension
extension StringParsing on String {
String changeDirection() {
return this.split('').reversed.join();
}
// ···
}
class Person {
String name = '';
Future<void> printOrderMessage() async {
print('Awaiting user order...');
var order = await fetchUserOrder();
print('Your order is: $order');
}
Future<String> fetchUserOrder() {
// Imagine that this function is more complex and slow.
return Future.delayed(const Duration(seconds: 4), () => 'Large Latte');
}
void main() async {
add2Numbers(int number1, {required int number2}) {
return number1 + number2;
}
add2NumbersNotReq([int number1 = 5, int number2 = 10]) {
return number1 + number2;
}
add2NumbersNamedOptional({required int number1, int? number2}) {
return number1 + (number2 ?? 0);