Skip to content

Instantly share code, notes, and snippets.

@mernen
Created August 20, 2023 20:19
Show Gist options
  • Save mernen/abd19bc8f0bf3db07116b1b037774cd3 to your computer and use it in GitHub Desktop.
Save mernen/abd19bc8f0bf3db07116b1b037774cd3 to your computer and use it in GitHub Desktop.
defaultTextStyleOf null dereference

defaultTextStyleOf null dereference

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: CustomButton(
onPressed: () {},
child: const Text('Custom button'),
),
),
),
);
}
}
class CustomButton extends TextButton {
const CustomButton({
super.key,
required super.onPressed,
required super.child,
});
@override
ButtonStyle defaultStyleOf(BuildContext context) {
return TextButton.styleFrom(
foregroundColor: Colors.teal,
// ...other custom styles here
// The following lines are now required to prevent null dereferences:
// enabledMouseCursor: SystemMouseCursors.click,
// disabledMouseCursor: SystemMouseCursors.basic,
).merge(super.defaultStyleOf(context));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment